You are using a closure inside of the callback to with
. First, a quote from that doc page:
Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct.
You are attempting to access a variable from the parent scope inside your anonymous function, so you must first pass it to the use
construct in order to let PHP know you are creating the closure around that variable:
$roomdata = Rooms::where('city_id','=',$citydata['id'])
->with('types')
->with('localities')
->with('slideshow')
->with(array('booking' => function($query) use($fdate) {
$query->where('bookstart','>',$fdate);
}))
;
Contrast this with Javascript, which allows a programmer to automatically access the parent scope because each child scope is part of the parent scope's Scope Chain. Something like this would be valid in Javascript:
var foo = 'bar';
(function() {
alert(foo);
})();
In PHP, gaining access to outer variables from within inner functions is not automatic. In this way, PHP differentiates between closures and anonymous functions (aka 'lambdas') in a way that JavaScript does not, at least syntax-wise. However, this is not clearly spelt out in the documentation (the manual seems to equate the two, when they're really not the same).