Hi, I have a global scope for some of my Models:
The Scope
class CompanyScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function apply(Builder $builder, Model $model)
{
$builder->whereCompanyId(session()->get('company_id'));
}
}
The Trait
trait ForCompany {
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::addGlobalScope(new CompanyScope);
}
}
And Model
class Amenity extends Model
{
use ForCompany;
}
Everything works fine when I query models from controller, the scope is executed, except when I use Route Model Binding.
RuntimeException in Request.php line 870: Session store not set on request.
So in my routes:
Route::group(['middleware' => ['web']], function () {
Route::resource('/amenities', 'AmenitiesController');
});
And my controller method:
/**
* Display the specified resource.
*
* @param \App\Amenity $amenity
* @return \Illuminate\Http\Response
*/
public function show(Amenity $amenity)
{
return response()->json($amenity);
}
What I am thinking is the bind is happening before the session is initialised. Is there any way to fix this?