repat / laravel-database-session-model

Laravel Eloquent Model for the sessions table
Other
1 stars 1 forks source link

Example? #2

Open jjjrmy opened 2 years ago

jjjrmy commented 2 years ago

Is there an example for retrieving the model? If you were to do like Session:find(session()->getId()), I think that is 2 queries? Is there an easier way?

Also it doesn't seem that the Session model is fillable? When I try Session::create([...]) it doesn't add anything.

repat commented 2 years ago

If you were to do like Session:find(session()->getId()), I think that is 2 queries?

Indeed.


Is there an easier way?

You could use the session cookie ID from the request(). It's usually encrypted by app/Http/Middleware/EncryptCookies.php but if you put your cookie name (config('session.cookie')) in the $except array, you see that the cookie value === sessions table id.

Sessions Table:

image

Chrome Developer Tools, Tab "Application", under "Storage/Cookies":

image


When I try Session::create([...]) it doesn't add anything.

Because that's not the way you're supposed to create sessions ;-) But feel free to extend the model and add $guarded = [] if you would like to create sessions this way.

jjjrmy commented 2 years ago

Because that's not the way you're supposed to create sessions ;-) But feel free to extend the model and add $guarded = [] if you would like to create sessions this way.

It was more for Database Testing, I see your point though. Would that be an accepted PR?

I made some changes with a modified database session driver, instead you can do:

protected function getQuery() {
    return new \App\Models\Session;
}

And that way you can return the entire model with some additions to the helper function like session()->model() Would this be an accepted PR?