Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
967 views
in Technique[技术] by (71.8m points)

eloquent - How to pass (start <= date1 and end >= date1) || (start <= date2 and end >= date2) condition in laravel

Hi every one i want to pass condition like this: (start <= date1 and end >= date1) || (start <= date2 and end >= date2) in laravel. how to pass with this query?

$events = DB::table('christophheich_calendar_entries')->whereNull('deleted_at');

Any one can helpe ? thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use where statement like the following:

$events = DB::table('christophheich_calendar_entries')
    ->where(function($q) use($date1, $date2) {
        $q->where(function($q2) use($date1) {
            $q2->where('start', '<=', $date1)->where('end', '>=', $date1);
        })->orWhere(function($q2) use($date2) {
            $q2->where('start', '<=', $date2)->where('end', '>=', $date2);
        });
    })
    ->whereNull('deleted_at');

This is how to create a where clause with a inner query that handles the or part of the where. Make sure $date1 and $date2 are initialised before the query.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...