私はユーザーとotpの間に1対多の関係を試みていますが、未知の列エラーがあります。 と私はユーザ認証のためにSentinelを使用しています。laravelクエリから未知の列を取得するEloquent
OTPモデル
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Sentinel;
class Otp extends Model
{
protected $fillable = ['user_id','phonenumber','otp'];
public function user()
{
return $this->belongsTo(Sentinel);
}
}
センチネルユーザモデル。
namespace Cartalyst\Sentinel\Users;
........
.......
protected $table = 'users';
/**
* {@inheritDoc}
*/
protected $fillable = [
'username',
'password',
'permissions',
'relationship',
'status',
'phone'
];
public function otp()
{
return $this->hasMany('App\Otp');
}
.....
OTPスキーマ
Schema::create('otps', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('phonenumber');
$table->string('otp');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});
センチネルユーザスキーマ。
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username');
$table->string('password');
$table->string('name')->nullable();
$table->bigInteger('phone')->nullable();
$table->integer('birth_month')->nullable();
$table->integer('birth_year')->nullable();
$table->integer('birth_day')->nullable();
$table->integer('relationship')->unsigned();
$table->integer('status')->nullable();
$table->text('permissions')->nullable();
$table->timestamp('last_login')->nullable();
$table->timestamps();
$table->engine = 'InnoDB';
$table->unique('username');
});
ので、そこに私は、ユーザーからのリクエストを受け付けています彼の電話番号が含まれていると私はOTPテーブル
public function phoneupdate(Request $request){
$this->validate($request, [
'phone' => 'bail|required|numeric|digits:10',
]);
$user = Sentinel::findById(3);
$randomOtp = rand (999 ,10000);
$user->otp()->create([
'phonenumber' => $request->phone,
'otp' => $randomOtp,
]);
return 'OK';
}
に保管しようとしていますが、エラー
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'eloquent_user_id' in 'field list'
(SQL: insert into `otps` (`phonenumber`, `otp`, `eloquent_user_id`, `updated_at`, `created_at`) values (1234567890, 5997, 3, 2016-12-23 11:34:55, 2016-12-23 11:34:55))
に与えます
eloquent_user_id
は、eloquent_user_id
の代わりにuser_id
である必要がありますが、laravel do私は
public function otp()
{
return $this->hasMany('App\Otp','user_id');
}
に
public function otp()
{
return $this->hasMany('App\Otp');
}
から
を変更した場合、それはデフォルトでなぜそれがこのエラーに
およびコードを与えているuser_id
外部キーを取るcumentationが正常に動作しますので、なぜ私デフォルトでuser_id
から取る場合は、user_id
を定義する必要があります。
** Eloquentは、モデル名に基づいてリレーションシップの外部キーを決定します。この場合、Phoneモデルは自動的にuser_id外部キーを持つとみなされます。この規約を無効にしたい場合は、hasOneメソッド**に2番目の引数を渡すことができます。電話クラスがあり、ドキュメントが外部キーを判断できると言うので、なぜsentinalの場合にはそうではないのですか?そこに見てみましょうhttps://laravel.com/docs/5.3/eloquent-relationships – codenut
私はバグではない代わりにキーを渡すavilableがあることを意味しますが、私はちょうどそれを知って好奇心です – codenut
私は正確ではない確かに、Sentinelをまったく使っていない。それはクラスのクールクかもしれません。 – aynber