2017-04-20 71 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();

希望这对你的工作!

相关问题