0

This work perfect:

public function scopeHBO($query)
{
    return $query ->where('network', '=', "hbo");

}

Call in Controller: It Works!

$events = Schedule::HBO()->orderBy('searchdate')->get();

When I add another Query Scope like so:

 public function scopeHBO($query)
{
    return $query
            ->where('network', '=', "hbo")
            ->where('searchdate', '>=', 'NOW()');
}

OR:

public function scopeDate($query)
{
    return $query->where('searchdate', '>= ', 'NOW()');
}

Then call in the controller:

$events = Schedule::HBO()->Date()->orderBy('searchdate')->get();

I get an error: Undefined variable: event. I tried with with Raw MySql in the same model and it works. Whenever i add a query scope, does not matter what it is.. i get that same error Undefined variable: event.

2
  • 2
    None of the code shown would cause Undefined variable: event. Commented Jan 12, 2015 at 20:20
  • No where in there are you using a variable named event. Commented Jan 12, 2015 at 20:21

2 Answers 2

2

NOW() is a function, so you need to use a raw query:

where('searchdate', '>=', DB::raw('NOW()'))

Then you can use the scopes. (Do note that I think scopeDate must be called as date(), not Date() - not 100 % sure on that though.)

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

1 Comment

This Worked... Thank you: return $query ->where('searchdate', '>=', DB::raw('NOW()'));
0

This sounds less like a generic problem with Laravel, and more like a problem with you specific application.

My guess (which is a wild guess), is that adding that second where clause in your scope method

return $query
        ->where('network', '=', "hbo")
        ->where('searchdate', '>=', 'NOW()');

ended up creating a SQL query that returned 0 rows. Then, somewhere in your other code you're doing something like

foreach($events as $event)
{
    //...
}

//referencing final $event outside of loop
if($event) { ... }

As I said, this is a wild guess, but the problem doesn't seem to be your query code, the problem seems to be the rest of your code that relies on the query returning a certain number of, or certain specific, rows/objects.

1 Comment

Yes that is what I'm doing in the view. It works just fine without the additional query scope. I think i was writing the added query scope wrong as what nhbjgfcxhgchg suggested worked.

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.