0

I have conditional code that joins a table depending on input params

    $content = Content::select('content.*');

    if (isset($filters['ruleNumber'])) {
        $content->join('violation', 'violation.content_id', '=', 'content.id')
                ->where('violation.rule_number', $filters['ruleNumber']);
    }

    if (isset($filters['unprocessed'])) {
        $content->join('violation', 'violation.content_id', '=', 'content.id')
                ->where('violation.status', 'pending');
    }

If both $filters['ruleNumber'] and $filters['unprocessed'] are due, it will result in a duplicate join query (i.e. "violation" is joined twice). I could keep track of my joins prior to calling join(), but I'd need to implement this logic wherever I use the query builder.

It seems odd that the framework doesn't support this out of the box, or am I missing something?

1 Answer 1

2

minor change i made.. try this it might be work

$content = Content::select('content.*');
if (isset($filters['ruleNumber']) || isset($filters['unprocessed'])){
    $content->join('violation', 'violation.content_id', '=', 'content.id')
}

if (isset($filters['ruleNumber'])) {
    $content->where('violation.rule_number', $filters['ruleNumber']);
}

if (isset($filters['unprocessed'])) {
    $content->where('violation.status', 'pending');
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.