我在保存Eloquent模型时遇到了问题。例如:PHP Laravel 5.4.21。雄辩地将插入保存在第一行
$game = Game::select(['bank', 'state', 'quantity_of_players'])->find($id)->first();
$game->bank += $game->bet;
$game->quantity_of_players++;
$game->save();
据我从文档知道,save()
应连续插入变化值与$id
,而是它在第一个行插入。什么是问题,以及如何将它正确保存到指定行中的数据库中。
迁移该型号:
public function up()
{
Schema::create('games', function (Blueprint $table) {
$table->bigIncrements('id');
$table->enum('state', ['wait', 'search', 'accept', 'play', 'finish']);
$table->smallInteger('quantity_of_players');
$table->smallInteger('ready_players');
$table->smallInteger('finished_players');
$table->integer('bank');
$table->smallInteger('bet');
});
}
会很感激的任何帮助。
P.S.所有SELECT
请求完美,如Eloquent create()
。
检查是否存在在调用此代码的包装函数中。有没有办法只得到选定的列,而不是所有的列? –
@JonnySmith因为你所做的只是更新,没关系。这是更好的保持代码清洁。如果你想选择列,然后检查我的编辑答案 – Sandeesh
问题仍然存在。它仍然插入第一个。是否可以将“id”放在$守护的位置? –