首页 > 解决方案 > 想用两个请求值查找

问题描述

我想用两个请求值更新我的列数据。我尝试了一个值。我不知道在哪里和方法。

Gold::where('type', '=', $request->type)->firstOrFail();

这仅适用于一个请求值查询搜索。但我想用 where query.how 用 laravel find 方法检查传入类型和 parent_id 的两个值。最后,我想更新检查两个值都包含数据并更新它的其他列值,就像这样。

$gold = Gold::where('type', '=', $request->type)->firstOrFail();
$gold->comment = $request->input('comment');
$gold->userid = $request->input('userid');
$gold->save();

标签: mysqllaravel

解决方案


根据您在问题评论中提供的 SQL 查询,这里是 Laravel 模型的解决方案:-

# this query will find your Gold item with there provided where conditions.
# you can pass more where conditions as per your requirements.
$gold = Gold::where('type', '=', $request->input('type'))
            ->where('parent_id', '=', $request->input('parent_id'))->first();

if(!$gold) {
    return throw Exception('Invalid Gold Item', 422);
}

$gold->comment = $request->input('comment');
$gold->userid = $request->input('userid');
$gold->save();

这是我理解的根据您的要求的解决方案。


推荐阅读