Php 创建和获取模型时出错
这个模型运行不正常。它不填充属性,我也不能调用Php 创建和获取模型时出错,php,laravel,orm,Php,Laravel,Orm,这个模型运行不正常。它不填充属性,我也不能调用find()、get(),或任何类型的getter 这是它的创建匹配表: public function up() { Schema::create('matches', function (Blueprint $table) { $table->increments('id'); $table->integer('p1'); $table->integer('p2');
find()
、get()
,或任何类型的getter
这是它的创建匹配表:
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]:一般错误:2031(SQL:select*from匹配
其中匹配Connection.php第729行中(?)中的id
我尝试过硬编码值,将其作为数组传递,甚至只是选择Match::first()
,都返回了这个错误
我所需要的就是让它像一个普通的ol'模型一样运行 不要在find()
之后链接first()
,因为find()
返回一个对象:
Match::find($id);
另外,确保$filleble
具有正确的属性,并且对于clock
属性,确保create()
工作:
public function setClockAttribute($value)
{
$this->attributes['clock'] = time() + 6;
}
我认为您的代码需要更新如下:
Match::find([$id])->first()代码>
到
Match::find($id)代码>
或
Match::where('id',$id)->first()代码>
希望这项工作为你 您是否配置了数据库设置?我没有使用$filleble
,而是尝试了$guarded=['id','created\u at']
。在删除此项(并修复查询生成器链)之后,它工作得很好。但导致此错误的是$guarded
。
public function setClockAttribute($value)
{
$this->attributes['clock'] = time() + 6;
}