Laravel 更新错误的模态
Posted
技术标签:
【中文标题】Laravel 更新错误的模态【英文标题】:Laravel updating wrong modals 【发布时间】:2021-07-04 15:51:59 【问题描述】:Laravel 正在更新错误的行 $winner_number is = 0
,但它正在更新 selectedItem
“绿色”。我不知道我在这里做错了什么。
// Laravel is updating wrong rows
$number_games = Game::where('period', $period)
->where('selectedItem', $winner_number)
->where('game_type_id', $game_type_id)->get();
// $number_games is empty but still its updating different modals
foreach ($number_games as $number_game)
$number_game->received_amount = $number_game->bet_amount *
$parameter->win_number_times;
$number_game->status = "success";
$number_game->save();
【问题讨论】:
【参考方案1】:我想忘了加上双引号。你在这里搜索一个字符串
$number_games = Game::where('period', $period)
->where('selectedItem', "$winner_number")
->where('game_type_id', "$game_type_id")->get();
foreach ($number_games as $number_game)
$number_game->received_amount = $number_game->bet_amount * $parameter->win_number_times;
$number_game->status = "success";
$number_game->save();
【讨论】:
【参考方案2】:我认为你可以在没有循环的情况下实现
$number_games = Game::where('period', $period)->where('selectedItem',
$winner_number)
->where('game_type_id', $game_type_id)->update([
'status'=>'success',
'received_amount'=>DB::raw(vsprintf('bet_amount* %s',[$parameter->win_number_times]))
])
【讨论】:
SQLSTATE[22007]:无效的日期时间格式:1292 截断不正确的 DOUBLE 值:“紫罗兰色”(SQL:更新games
设置 status
= 成功,games
.updated_at
= 2021- 07-04 22:18:56 其中period
= 20210704438 和selectedItem
= 0 和game_type_id
= 1)
@Ganeshrao 你能展示一下游戏模型和数据库表结构吗
我的模型 belongsTo(User::class); 公共函数 game_type() return $this->belongsTo(GameType::class); public function transaction() return $this->hasOne(Transaction::class);
Schema::create('games', function (Blueprint $table) $table->id(); $table->string('period'); $table->string(' selectedItem'); $table->string('total_amount'); $table->string('bet_amount'); $table->string('service_charge'); $table->string('received_amount')->可为空(); $table->string('status')->default('waiting');
$table->unsignedBigInteger('game_type_id'); $table->unsignedBigInteger('user_id'); $table->foreign('game_type_id')->references('id')->on('game_types')->onDelete('cascade'); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); $table->timestamps();以上是关于Laravel 更新错误的模态的主要内容,如果未能解决你的问题,请参考以下文章