0
0

More than 1 year has passed since last update.

laravel ログイン userテーブル id変更

Last updated at Posted at 2022-12-15

laravelのログイン機能はデフォルトでuserテーブルのidを参照するようになっている。
従い、idではない名前、例えばuser_idをプライマリーキーとして参照したい場合は1手間加える必要がある。

●変更するファイル
「プロジェクトフォルダ」->app->User.php

●User.phpに追記する内容

protected $primaryKey = 'user_id';

●例

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'kana', 'tel',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    // userテーブルのプライマリーキーはuser_id
    protected $primaryKey = 'user_id';
}

●参考
https://zenn.dev/miyapei/articles/725316f6c52a579f2f00

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0