Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sebastian.virlan's avatar

Session store not set on request - In Global Scope

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?

0 likes
2 replies
sebastian.virlan's avatar
Level 1

Solved, I had to move:

\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class

from protected $middlewareGroups in protected $middleware in Kernel.php

But does this impact in anyway the application?

AndrewMast's avatar

It shouldn't impact anything unless you have it in both $middlewareGroups and $middleware.

1 like

Please or to participate in this conversation.