2016-12-23 14 views
0

私はユーザーと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を定義する必要があります。

答えて

1

ユーザークラスではなくSentinelを渡すので、参加している列を送信する必要があります。

public function user() 
{ 
    return $this->belongsTo(Sentinel, 'id', 'user_id'); 
} 

これで解決しない場合は、Sentinel \ Userクラスに列を追加してみてください。

public function otp() 
{ 
    return $this->hasMany('App\Otp', 'user_id'); 
} 

ソースコードにダイビング、​​たUserInterfaceは、モデルCartalyst\Sentinel\Users\EloquentUserを使用していますので、外部キーを決定するために、そのモデル名を使用しています。

+0

** Eloquentは、モデル名に基づいてリレーションシップの外部キーを決定します。この場合、Phoneモデルは自動的にuser_id外部キーを持つとみなされます。この規約を無効にしたい場合は、hasOneメソッド**に2番目の引数を渡すことができます。電話クラスがあり、ドキュメントが外部キーを判断できると言うので、なぜsentinalの場合にはそうではないのですか?そこに見てみましょうhttps://laravel.com/docs/5.3/eloquent-relationships – codenut

+0

私はバグではない代わりにキーを渡すavilableがあることを意味しますが、私はちょうどそれを知って好奇心です – codenut

+0

私は正確ではない確かに、Sentinelをまったく使っていない。それはクラスのクールクかもしれません。 – aynber

関連する問題