我使用三个表备件,类别和参数和YajraDatatables。
我的控制器动作是:
public function anyData()
{
$spareparts_param_list = Sparepart::all();
$list='';
foreach ($spareparts_param_list as $value) {
foreach ($value->category->parameter as $par_list) {
$list .= $par_list->Name.',';
}
}
$spareparts = Sparepart::
join('cars', 'spareparts.car_id', '=', 'cars.id')
->select(['spareparts.id', 'cars.Brend', 'spareparts.Model', $list]);
$datatables = app('datatables')->of($spareparts);
return $datatables->make();
}
我的数组列表 $ list 打印参数,例如颜色,类型,轮胎,。 如何在选择查询中传递 $ list 数组?
答案 0 :(得分:1)
您可以使用 whereIn(),如下所示:
public class ControllerTop {
@FXML public Button topbtn;
@FXML public Button bottombtn;
@FXML
public void printOutput() {
System.out.println("Hello from top button");
topbtn.setDisable(true); //OK!
bottombtn.setDisable(false); //Failed
}
}
答案 1 :(得分:1)
您可以使用whereIn
:
public function anyData()
{
$spareparts_param_list = Sparepart::all();
$list = [];
foreach ($spareparts_param_list as $value) {
foreach ($value->category->parameter as $par_list) {
$list[] = $par_list->Name;
}
}
$spareparts = Sparepart::
join('cars', 'spareparts.car_id', '=', 'cars.id')
->whereIn('Name', $list)
->select(['spareparts.id', 'cars.Brend', 'spareparts.Model']);
$datatables = app('datatables')->of($spareparts);
return $datatables->make();
}
您可以运行以下原始Mysql
命令:
select sp.id as sp_id, sp.model as sp_model, c.brend as car_brend, json_arrayagg(p.name) as p_name
from spareparts as sp
join cars as c on sp.car_id=c.id
join categories as cat on sp.category_id=cat.id
join parameters as p on cat.id=p.category_id
group by sp.model;
以上命令将为您提供如下结果:
1 | Audi A6 door | Audi A6 | ["color", "window"]
最后一列将是与每个类别相关的所有参数的json列。