1

How to make a whereclause that is based on an existing field in the database, and not on an input parameter?

$query = DB::table('events')
    ->join('events_dates', function($join) use ($data){ 
                $join->on('events.id', '=', 'events_dates.event_id'); 
                    $join->where('events_dates.start_date', "<=", $data['date_end']); 
                    $join->where('events_dates.end_date', '>=', $data['date_start']); 
}); 

This works well because the where clause is based on an input parameter.
What I need is a Where clause that is based on a field that is already in the database:

Something like this:

$query = DB::table('events')
    ->join('events_dates', function($join) use ($data){ 
                $join->on('events.id', '=', 'events_dates.event_id'); 
                //If db field of record: recurrent == 0 then 
                    $join->where('events_dates.start_date', "<=", $data['date_end']); 
                    $join->where('events_dates.end_date', '>=', $data['date_start']); 
                /* If db field of record: "recurrent" == "1" then
                    $join->where //another query 
                */ 
}); 

Is this achievable with the laravel ORM, or should I write a native SQL query?
Haven't found a suitable answer in the docs or in existing posts.

1 Answer 1

2

You need to use...

where('column1', '=', DB::raw('column2'));

...to use the field value instead of the string "column2".

In this answer I further explained why.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok, should I use a nested where within the join clause to fulfil the requirement posed in the question? I will do some tests to see how it works out.
That's basically the question you posted here right? I will take a look at it and answer there if I find a solution. It's rather off topic here (considering the question title)
Yes, it refers to the same question, but I assumed that the previous question might not have been straightforward enough. Thanks for your effort, I have been searching for a few hours today but have been unable to get it right so far.
Would you please consider accepting this answer? You have to admit, it answers the question (even if it didn't help you much ;))

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.