gpt4 book ai didi

Laravel - Eloquent : Advanced Wheres

转载 作者:行者123 更新时间:2023-12-04 13:11:23 27 4
gpt4 key购买 nike

我正在尝试使用 Eloquent 创建类似的东西。但是,我在使用 or 子句时遇到了麻烦。

SELECT * FROM table WHERE column1 = 1 AND column2 = 2 AND (column3 LIKE $search_term.'%' OR column4 LIKE $search_term.'%') ORDER BY column1;

这是我的:

$query = DB::table('table')
->where('column1', '=', 1)
->where('column2', '=', 2)
->orderBy('column1', 'asc');

$results = App\Table::where(function ($query) use ($search_term) {
$query->where('column3', 'like', $search_term.'%');
})->orwhere(function ($query) use ($search_term) {
$query->where('column4', 'like', $search_term.'%');
});

最佳答案

您的查询应如下所示:

$query = DB::table('table')
->where('column1', 1)
->where('column2', 2)
->where(function ($q) use ($search_term) {
$q->where('column3', 'like', $search_term.'%')
->orWhere('column4', 'like', $search_term.'%');
})
->orderBy('column1', 'asc')
->get();

关于Laravel - Eloquent : Advanced Wheres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41463262/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com