У меня есть такой код:
$result = DB::table('product as p')
->join('master as cl', 'p.color', '=', 'cl.name')
->join('master as brand', 'p.brand', '=', 'brand.name')
->where('p.brand', $brandname)
->whereRaw("p.del_flag = 0 and cl.is_active = 1 and brand.is_active = 1 and p.product_type = 1")
->select('p.name')
->groupBy('p.name')
->orderBy('p.name')
->take(3)
->get();
Мне нужно добавить фильтры:
if (isset($_POST['brand'])) {
->where('p.brand', $_POST['brand'])
}
if (isset($_POST['color'])) {
->where('p.color', $_POST['color'])
}
if (isset($_POST['product_type'])) {
->where('p.product_type', $_POST['product_type'])
}
Я видел много сообщений о множестве Where в laravel, но ни в одном из них не говорилось об этих особых местах.
как добавить фильтр в запрос?






Сначала передайте запрос переменной и обновите переменную запроса.
$query = DB::table('product as p')
->join('master as cl', 'p.color', '=', 'cl.name')
->join('master as brand', 'p.brand', '=', 'brand.name')
->where('p.brand', $brandname)
->whereRaw("p.del_flag = 0 and cl.is_active = 1 and brand.is_active = 1 and p.product_type = 1")
->select('p.name')
->groupBy('p.name')
->orderBy('p.name')
->take(3);
if ( isset($_POST['brand'])) {
$query->where('p.brand', $_POST['brand'])
}
if (isset($_POST['color'])) {
$query->where('p.color', $_POST['color'])
}
if (isset($_POST['product_type'])) {
$query->where('p.product_type', $_POST['product_type'])
}
$result = $query->get();
Обновлять:
Как упоминал @Amol Rokade, вы также можете использовать функцию when():
$result = DB::table('product as p')
->join('master as cl', 'p.color', '=', 'cl.name')
->join('master as brand', 'p.brand', '=', 'brand.name')
->where('p.brand', $brandname)
->whereRaw("p.del_flag = 0 and cl.is_active = 1 and brand.is_active = 1 and p.product_type = 1")
->when(isset($_POST['brand']), function ($query) {
$query->where('p.brand', $_POST['brand']);
})
->when(isset($_POST['color']), function ($query) {
$query->where('p.color', $_POST['color']);
})
->when(isset($_POST['product_type']), function ($query) {
$query->where('p.product_type', $_POST['product_type']);
})
->select('p.name')
->groupBy('p.name')
->orderBy('p.name')
->take(3)
->get();
Можем ли мы использовать вместо вышеуказанного laravel.com/docs/5.7/queries#conditional-clauses?
если вы хотите добавить фильтр в запрос, вы можете сделать это, создав объект запроса что-то вроде этого
$result = DB::table('product as p')
->join('master as cl', 'p.color', '=', 'cl.name')
->join('master as brand', 'p.brand', '=', 'brand.name')
->where('p.brand', $brandname)
->whereRaw("p.del_flag = 0 and cl.is_active = 1 and brand.is_active = 1 and p.product_type = 1")
->select('p.name')
->groupBy('p.name')
->orderBy('p.name');
if (isset($_POST['brand'])) {
$result->where('p.brand', $_POST['brand'])
}
if (isset($_POST['color'])) {
$result->where('p.color', $_POST['color'])
}
if (isset($_POST['product_type'])) {
$result->where('p.product_type', $_POST['product_type'])
}
$result->take(3)->get();
это поможет.
Попробуй это
$query = DB::table('product as p')
->join('master as cl', 'p.color', '=', 'cl.name')
->join('master as brand', 'p.brand', '=', 'brand.name')
->where(function($query) use ($brandname,$_POST){
$query->where('p.brand', $brandname);
$query->whereRaw("p.del_flag = 0 and cl.is_active = 1 and brand.is_active = 1 and p.product_type = 1");
if ( isset($_POST['brand'])) {
$query->where('p.brand', $_POST['brand']);
}
if (isset($_POST['color'])) {
$query->where('p.color', $_POST['color']);
}
if (isset($_POST['product_type'])) {
$query->where('p.product_type', $_POST['product_type']);
}
})
->select('p.name')
->groupBy('p.name')
->orderBy('p.name')
->take(3);
->get();
Надеюсь, что это поможет вам
Мой последний код:
$querydata = DB::table('product as p')
->join('master as cl', 'p.color', '=', 'cl.name')
->join('master as brand', 'p.brand', '=', 'brand.name')
->where('p.brand', $brandname)
->whereRaw("p.del_flag = 0 and cl.is_active = 1 and brand.is_active = 1 and p.product_type = 1")
->select('p.name')
->groupBy('p.name')
->orderBy('p.name')
->take(3);
if (!empty($_POST)) {
unset($_POST['_token']);
foreach ($_POST as $column => $data) {
if (empty($data)) {
continue;
}
if (is_array($data)) {
$querydata = $querydata->where(function($query) use ($column, $data) {
foreach ($data as $value) {
$query->orWhere('p.'.$column, $value);
}
});
} else {
$querydata->where('p.'.$column, $data);
}
}
}
$result = $querydata->get();
Надеюсь кому-нибудь помочь.
Попробуй это laravel.com/docs/5.7/queries#conditional-clauses