我有一個SQL查詢,我想根據不同的場景傳遞不同的參數。我不知道如何做到這一點。這是查詢:PHP - 將參數從數組傳遞到SQL查詢
$data = Engagement::leftJoin('user_engagement', 'engagements.id', '=', 'user_engagement.engagement_id')
->select('engagement_id', 'engagements.question', 'engagements.id')
->selectRaw('count(*) as count')
->whereBetween('user_engagement.created_at', [$from.' 00:00:00', $to.' 00:00:00'])
->groupBy('user_engagement.engagement_id')
->orderBy('count', 'DESC')
->take(5)
->get()->toArray();
所以,我想這樣做什麼,會是這個樣子:
$model = new Engagement;
$leftJoin = ['user_engagement', 'engagements.id', '=', 'user_engagement.engagement_id'];
$select = ['engagement_id', 'engagements.question', 'engagements.id'];
$whereBetween = 'user_engagement.created_at';
$groupBy = 'user_engagement.engagement_id';
$data = $model->leftJoin($leftJoin)
->select($select)
->selectRaw('count(*) as count')
->whereBetween($whereBetween, [$from.' 00:00:00', $to.' 00:00:00'])
->groupBy($groupBy)
->orderBy('count', 'DESC')
->take(5)
->get()->toArray();
我想你可以在$ model對象上使用[call_user_func_array](http://php.net/manual/en/function.call-user-func-array.php)並且使用技巧 –
保重sql注入.. – Fky
我不能在模型對象上使用我調用未定義的方法Illuminate \ Database \ Query \ Builder :: call_user_func_array() ''' – Marco