0

How can I write this kind of queries in laravel 5.2 eloquent or query builder?

$query = Customers::leftjoin('Query string');    
if(condition)
    {
        //This part added to query   
    }
    else
    {
        //This part added to query
    }
->get();
1

1 Answer 1

4

You have to remember your query in a variable, I.E. like this:

$query = Customers::leftjoin('Query string');   

if(condation){
    $query = $query->where('something', '=', 'something');
} else {
    $query = $query->where('somethingelse', '=', 'somethingelse');
}

$query = $query->get();

Normally you would have $query->where()-get();, which is basically the same thing, $query will hold the result of your ->where() and allows you to chain further on $query

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.