动态laravel SQL查询生成器

时间:2015-06-25 11:38:50

标签: php laravel

我正在尝试使用laravel创建一个动态查询,如果满足某些条件,它将添加到我的查询中。这是我到目前为止所尝试的。有什么指导吗?

    $Tasks = Task::leftJoin('task_recipients', 'tasks.task_id', '=', 'task_recipients.recipient_task_id');

    if ($filterAssignedToMe !== NULL) {
        $Tasks->where('task_recipients.recipient_user_id', '=', $user_id);
    }

    if ($filterAssignedByMe !== NULL) {
        $Tasks->where('tasks.created_by', '=', $user_id);
    }

    $Tasks->groupBy('task_id');
    $Tasks->get();

1 个答案:

答案 0 :(得分:0)

你走了:

$Tasks = Task::leftJoin('task_recipients', 'tasks.task_id', '=', 'task_recipients.recipient_task_id');

if ($filterAssignedToMe !== NULL) {
    $Tasks = $Tasks->where('task_recipients.recipient_user_id', '=', $user_id);
}

if ($filterAssignedByMe !== NULL) {
    $Tasks = $Tasks->where('tasks.created_by', '=', $user_id);
}

$Tasks = $Tasks->groupBy('task_id');
$Tasks = $Tasks->get();

这有用吗?