2017-04-20 72 views
1

此模型行爲不正確。它沒有填寫它的屬性,也不能撥打find()get()或任何類型的吸氣劑。創建和獲取模型的錯誤

這裏是它的create_matches_table:

public function up() 
{ 
    Schema::create('matches', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('p1'); 
     $table->integer('p2'); 
     $table->integer('u1'); 
     $table->integer('u2'); 
     $table->string('p1_action')->nullable(); 
     $table->string('p2_action')->nullable(); 
     $table->bigInteger('clock')->unsigned(); 
     $table->smallinteger('round')->unsigned()->default('1'); 
     $table->integer('victor')->nullable(); 
     $table->boolean('murder'); 
     $table->integer('wager')->unsigned()->nullable(); 
     $table->integer('notoriety')->nullable(); 
     $table->integer('stalemate')->default('0'); 
     $table->timestamps(); 
     $table->datetime('finished_at')->nullable(); 
    }); 
} 

使用Match::create($data)其中$data是屬性的數組完美排隊與那些需要將不會填充它們返回的對象從不具有clock屬性如上述所希望的。

public static function setMatch($data) 
{ 

    $match = new Match; 
    $match->fill($data); 
    $match->clock = time() + 6; 
    $match->save(); 

    return $match; 
} 

即使是在這樣做,試圖讓這個對象由$match = Match::find([$id])->first();的特定記錄產生這個錯誤:

SQLSTATE[HY000]: General error: 2031 (SQL: select * from matches where matches . id in (?)) in Connection.php line 729

at 
> Connection->runQueryCallback('select * from `matches` where 
> `matches`.`id` in (?)', array(), object(Closure)) in Connection.php 
> line 685 at Connection->run('select * from `matches` where 
> `matches`.`id` in (?)', array(), object(Closure)) in Connection.php 
> line 349 at Connection->select('select * from `matches` where 
> `matches`.`id` in (?)', array(), true) in Builder.php line 1610 

我已經勉強通過繞行這些問題我嘗試了硬編碼值,將它作爲一個數組傳遞,甚至只是爲了Match::first(),並且都返回這個錯誤。

所有我需要的是爲此作爲一個常規的醇模型。

+0

你配置你的數據庫設置嗎? –

回答

2

不要鏈first()因爲find()find()後返回一個對象:

Match::find($id); 

此外,確保$fillable具有正確的屬性和define a mutatorclock屬性,使create()工作:

public function setClockAttribute($value) 
{ 
    $this->attributes['clock'] = time() + 6; 
} 
+0

而不是使用'$ fillable',我嘗試過'$ guarded = ['id','created_at']'。刪除這個(並修復查詢生成器鏈)後,它工作得很好。儘管如此,這是導致這個錯誤的「守護」。 – Naltroc

0

我想您的代碼需要更新,如:

Match::find([$id])->first();

Match::find($id);

或者

Match::where('id',$id)->first();

希望這對你的工作!

相關問題