laravel / folio

Page based routing for Laravel.
MIT License
568 stars 46 forks source link

Session not set in Global Scope #96

Closed PerryvanderMeer closed 1 year ago

PerryvanderMeer commented 1 year ago

Folio Version

1.0.0

Laravel Version

10.20.0

PHP Version

8.2.4

Description

Folio returns a 404 when using a session within a Global Scope in an Eloquent model. session('test') returns null; in plain Laravel it returns the actual session data.

Steps To Reproduce

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Database\Eloquent\Builder;

class Test extends Model
{
    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope('test', fn (Builder $builder) => $builder->where('test', session('test')));
    }
}
// folio/[Test]/edit.blade.php

{{ $test->id }}
jdion84 commented 1 year ago

use view composer to pass session data to views via service provider:

public function boot()
{
    view()->composer('*', function ($view) {
        $view->with('test', session('test'));    
    });  
}
nunomaduro commented 1 year ago

Hey there,

While this may be a legitimate issue, can you first try posting your problem or question on one of the support channels below? Also do check if manually running the artisan command would fix the issue, which might mean that it only possible to run psql on CLI and not web requests.

If this issue can be definitively identified as a bug, feel free to open up a new issue with a link to the original one and we'll gladly help you out.

Thanks!

PerryvanderMeer commented 1 year ago

Thanks, @nunomaduro. I suspect that the problem is caused by the fact that the session has not yet started. The session is not started until https://github.com/laravel/folio/blob/5ade0ba20d6b85c88d386758d10239f9b7cb271b/src/RequestHandler.php#L67C1-L73C20

While the route is being resolved in https://github.com/laravel/folio/blob/5ade0ba20d6b85c88d386758d10239f9b7cb271b/src/RequestHandler.php#L38C39-L38C39