我在保存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」放在$守護的位置? –