0

I'm trying to find records by time interval. In table there are columns like 'date_from' and 'date_to', those are specify starts and ends date of an event.

public function scopeByTimeInterval($query, $dateInterval)
{
    $query->where(function ($query) use ($dateInterval) {
        [$from, $to] = $dateInterval;
        $query->where([
            ['date_from', '<=', $from],
            ['date_to', '>=', $to]
        ]);
        $query->orWhere([
            ['date_from', '>=', $from],
            ['date_to', '<=', $to]
        ]);
        $query->orWhereBetween('date_from', $dateInterval);
        $query->orWhereBetween('date_to', $dateInterval);
    });
}

when I use where query directly, there are no problem. I can see all events between those dates. But if I use it as a scope, it returns me every events in given year and month and not the interval..

What might cause kind of behavior ? Or am I missing something ?

3
  • Your query is basically selecting everything! Can you share what are you trying to do? Commented Jan 11, 2020 at 15:44
  • Hi @Rwd I'm appriciate for your reply, but unfo, your queries returns very mistaken results. Commented Jan 18, 2020 at 14:33
  • In that case please can you say/share what you're trying to achieve, or explain how/why the answers aren't giving you the results you want. This way you're more likely to get the help you're looking for :) Commented Jan 18, 2020 at 14:46

2 Answers 2

2

As mentioned in the comments, your query is essentially selecting everything. If you're trying to get the events that are happening during the $from and $to dates you could do something like:

public function scopeDateInterval($query, $interval)
{
    [$from, $to] = $interval;

    $query
        ->where(function ($query) use ($from, $to) {
            $query
                ->where(function ($query) use ($from) {
                    $query
                        ->where('date_from', '<=', $from)
                        ->where('date_to', '>=', $from);
                })
                ->orWhere(function ($query) use ($to) {
                    $query
                        ->where('date_from', '<=', $to)
                        ->where('date_to', '>=', $to);
                })
                ->orWhere(function ($query) use ($from, $to) {
                    $query
                        ->where('date_from', '>=', $from)
                        ->where('date_to', '<=', $to);
                });

        });
}

The above is basically saying where the $from or $to is between the start and end dates, or the start and end dates are between the $from and $to.

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

Comments

-1

Think you miss return $query->where...

2 Comments

Could you expand your answer to explain?
You don't need to return from scopes.

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.