选择查询中的Laravel传递列表参数(列)

时间:2019-02-17 13:06:53

标签: php arrays laravel parameter-passing yajra-datatable

我使用三个表备件类别参数和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 数组?

2 个答案:

答案 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
    }
}

文档-https://laravel.com/docs/5.7/queries#where-clauses

答案 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列。