Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I save Objects, Object Collections in a Laravel 5 Session?

Is it possible to save php Objects, object collections in laravel 5 Session?

I was trying but I get error on this

Serialization of 'Closure' is not allowed vendor/illuminate/session/Store.php line 255

Session::put('my_php_object', $obj );
Session::save();


public function onRun()
{

    $this->addCss('assets/css/custom.css');
    $this->socialite_providers = $this->page['socialite_providers'] =$this->providersList();

    //check for provider param in url
    if($provider = $this->param('provider')){

        $this->setSessionProvider($provider);
        $this->provider = $provider;
        $this->callback_url = preg_replace('~.*\K:(.*)~s','',Request::root().$this->page->url);
        $this->request = $this->createRequest($provider);


        Session::save();
        return $this->request->redirect();

    }

    //Authorize user if Request has code
    if(Request::has('code')){

        if(!$this->getSession())
            return;

        //reuse save session

    }

}

public function createRequest($provider)
{
    $instance = Socialite::driver($provider);
    $init = $this->injectCredentials($instance, $provider);

    $this->setSession($init);
    return $init;
}

public function injectCredentials($instance, $provider){
    $credential = $this->providerData($provider)->toArray();
    $instance = new $instance
    (
        Request::instance(),
        $credential['client_id'],
        $credential['client_secret'],
        $this->callback_url
    );

    return $instance;
}

public function setSession($init)
{
    if(Session::has('socialite_object'))
        Session::forget('socialite_object');


    Session::put('socialite_object', $init );

}
like image 680
fefe Avatar asked Mar 24 '15 16:03

fefe


People also ask

Where session is stored in laravel?

Laravel ships with several great drivers out of the box: file - sessions will be stored in storage/framework/sessions . cookie - sessions will be stored in secure, encrypted cookies. database - sessions will be stored in a database used by your application.

How to handle session in Laravel?

To access the session data, we need an instance of session which can be accessed via HTTP request. After getting the instance, we can use the get() method, which will take one argument, “key”, to get the session data.

How do you set a session variable in laravel?

The correct syntax for this is: Session::set('variableName', $value); For Laravel 5.4 and later, the correct method to use is put : Session::put('variableName', $value);


1 Answers

You could always store the JSON representation of the object you tried to store.

However, I've heard it's generally a good idea not to store large things in the Session, is there anyway you could manage by storing something else? If you are storing an object from the DB, you could store the Id and recover the object with a query.

like image 193
Martin Rifon Avatar answered Oct 11 '22 19:10

Martin Rifon