laravelでgithub認証をし、認証が通ったユーザをuser
テーブルにinsertしようとすると、ローカルではエラーなく、意図通りの動きになるのですが、
Herokuでやると何故か表題に記載のエラーが発生し、データ書き込み時に項目が存在しないというエラーが返ってきます。
期待している動き
ローカルと同じく、問題なくユーザが追加し、リダイレクト先の画面が正常に表示される。
エラーメッセージ詳細
SQLSTATE[42703]: Undefined column: 7 ERROR: column "UPDATED_AT" of relation "user" does not exist LINE 1: insert into public.user (github_id, "created_at", "UPDATED_A... ^ (SQL: insert into public.user (github_id, "created_at", "UPDATED_AT") values (moriokameda,20/06/21 16:16:31,20/06/21 16:16:31))
試したこと
・insert文の項目名を全て大文字にしてみる。
・insert文の該当の項目名に""を入れる。
・insert文の該当の項目名を削除してみる
herokuの設定
DB_HOST
DB_CONNECTION
DB_PORT
DB_DATABASE
DB_USER
DB_PASSWORD
GITHUB_CLIENT_ID
GITHUB_CLIENT_SECRET
GITHUB_REDIRECT
APP_KEY
ソースコード
LoginController.php
<?php namespace App\Http\Controllers\Auth; use App\Http\Controllers\Controller; use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Socialite; class LoginController extends Controller { /* |-------------------------------------------------------------------------- | Login Controller |-------------------------------------------------------------------------- | | This controller handles authenticating users for the application and | redirecting them to your home screen. The controller uses a trait | to conveniently provide its functionality to your applications. | */ use AuthenticatesUsers; /** * Where to redirect users after login. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest')->except('logout'); } /** * githubの認証ページへリダイレクト */ public function redirectToProvider() { return Socialite::driver('github')->scopes(['read:user', 'public_repo'])->redirect(); } /** * githubからユーザ情報を取得 * * @return \Illuminate\Http\Response */ public function handleProviderCallback(Request $request) { $github_user = Socialite::driver('github')->stateless()->user(); $now = date('y/m/d H:i:s'); $app_user = DB::select('select * from public.user where github_id = ?', [$github_user->user['login']]); if (empty($app_user)) { # code... DB::insert('insert into public.user (github_id,created_at,updated_at) values (?,?,?)', [$github_user->user['login'], $now, $now]); } $request->session()->put('github_token', $github_user->token); return redirect('github'); } }
GithubController.php
<?php namespace App\Http\Controllers\Github; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Socialite; class GithubController extends Controller { public function top(Request $request) { $token = $request->session()->get('github_token', null); try { $github_user = Socialite::driver('github')->userFromToken($token); } catch (\Exception $e) { //throw $th; return redirect('login/github'); } $client = new \GuzzleHttp\Client(); $res = $client->request('GET', 'https://api.github.com/user/repos', [ 'auth' => [$github_user->user['login'], $token], ]); $app_user = DB::select('select * from public.user where github_id = ?', [$github_user->user['login']]); return view('github', [ 'user' => $app_user[0], 'nickname' => $github_user->nickname, 'token' => $token, 'repos' => array_map(function ($o) { return $o->name; }, json_decode($res->getBody())), ]); } public function createIssue(Request $request) { $token = $request->session()->get('github_token', null); $user = Socialite::driver('github')->userFromToken($token); $client = new \GuzzleHttp\Client(); $res = $client->request('POST', 'https://api.github.com/repos/' . $user->user['login'] . '/' . $request->input('repo') . '/issues', [ 'auth' => [$user->user['login'], $token], 'json' => [ 'title' => $request->input('title'), 'body' => $request->input('body'), ] ]); return view('done', [ 'response' => json_decode($res->getBody())->html_url ]); } }
web.php
<?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Route::get('/user', "UserController@index"); // 掲示板 Route::get('/bbs', 'BbsController@index'); Route::post('/bbs', 'BbsController@create'); // github_login Route::get('github', 'Github\GithubController@top'); Route::post('github/issue', 'Github\GithubController@createIssue'); Route::get('login/github', 'Auth\LoginController@redirectToProvider'); Route::get('login/github/callback', 'Auth\LoginController@handleProviderCallback'); Route::post('user', 'User\UserController@updateUser'); Route::get('/test', "TestController@index"); // Photo Route::get('/home', 'HomeController@index'); Route::post('/upload', 'HomeController@upload');
service.php
<?php return [ /* |-------------------------------------------------------------------------- | Third Party Services |-------------------------------------------------------------------------- | | This file is for storing the credentials for third party services such | as Mailgun, SparkPost and others. This file provides a sane default | location for this type of information, allowing packages to have | a conventional file to locate the various service credentials. | */ 'github' => [ 'client_id' => env('GITHUB_CLIENT_ID'), 'client_secret' =>env('GITHUB_CLIENT_SECRET'), // 'redirect' => '/login/github/callback', 'redirect' => env('GITHUB_REDIRECT'), ], 'mailgun' => [ 'domain' => env('MAILGUN_DOMAIN'), 'secret' => env('MAILGUN_SECRET'), 'endpoint' => env('MAILGUN_ENDPOINT', 'api.mailgun.net'), ], 'postmark' => [ 'token' => env('POSTMARK_TOKEN'), ], 'ses' => [ 'key' => env('AWS_ACCESS_KEY_ID'), 'secret' => env('AWS_SECRET_ACCESS_KEY'), 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), ], 'sparkpost' => [ 'secret' => env('SPARKPOST_SECRET'), ], ];
2020_06_18_120744_create-table.php
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { // Schema::create('user', function (Blueprint $table) { $table->increments('id'); $table->string('name')->nullable(); $table->string('comment')->nullable(); $table->string('github_id'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { // Schema::drop('user'); } }
環境
laravel 5.8
php 7.3
postgres 10.18 server 12
laradockで構築
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。