Multi session tables in Laravel











up vote
0
down vote

favorite












i have setup a Laravel project with two guards wach with Hi own users table. Now i want to save the sessions into the database instead in a file. The Problem is that I cant save the session in a single table, because the user ID is not unique.
How can I solve this problem? Thanks.










share|improve this question







New contributor




Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • If you have a look at config/session.php, you'll notice 'connection' => null and 'table' => 'sessions'; those will have to be updated on the fly by your guards to connect to the correct connection, and/or the correct table based on your logged in user. I'm not sure if there's a way to do that out of the box, but Laravel does note that you can set them on the fly: laravel.com/docs/5.7/…
    – Tim Lewis
    Nov 21 at 19:15










  • But I do not know, where should I change the table. Because the session starts before I have access to the authorized user.
    – Hacko
    2 days ago















up vote
0
down vote

favorite












i have setup a Laravel project with two guards wach with Hi own users table. Now i want to save the sessions into the database instead in a file. The Problem is that I cant save the session in a single table, because the user ID is not unique.
How can I solve this problem? Thanks.










share|improve this question







New contributor




Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • If you have a look at config/session.php, you'll notice 'connection' => null and 'table' => 'sessions'; those will have to be updated on the fly by your guards to connect to the correct connection, and/or the correct table based on your logged in user. I'm not sure if there's a way to do that out of the box, but Laravel does note that you can set them on the fly: laravel.com/docs/5.7/…
    – Tim Lewis
    Nov 21 at 19:15










  • But I do not know, where should I change the table. Because the session starts before I have access to the authorized user.
    – Hacko
    2 days ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











i have setup a Laravel project with two guards wach with Hi own users table. Now i want to save the sessions into the database instead in a file. The Problem is that I cant save the session in a single table, because the user ID is not unique.
How can I solve this problem? Thanks.










share|improve this question







New contributor




Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











i have setup a Laravel project with two guards wach with Hi own users table. Now i want to save the sessions into the database instead in a file. The Problem is that I cant save the session in a single table, because the user ID is not unique.
How can I solve this problem? Thanks.







php database laravel session






share|improve this question







New contributor




Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 21 at 18:51









Hacko

11




11




New contributor




Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • If you have a look at config/session.php, you'll notice 'connection' => null and 'table' => 'sessions'; those will have to be updated on the fly by your guards to connect to the correct connection, and/or the correct table based on your logged in user. I'm not sure if there's a way to do that out of the box, but Laravel does note that you can set them on the fly: laravel.com/docs/5.7/…
    – Tim Lewis
    Nov 21 at 19:15










  • But I do not know, where should I change the table. Because the session starts before I have access to the authorized user.
    – Hacko
    2 days ago


















  • If you have a look at config/session.php, you'll notice 'connection' => null and 'table' => 'sessions'; those will have to be updated on the fly by your guards to connect to the correct connection, and/or the correct table based on your logged in user. I'm not sure if there's a way to do that out of the box, but Laravel does note that you can set them on the fly: laravel.com/docs/5.7/…
    – Tim Lewis
    Nov 21 at 19:15










  • But I do not know, where should I change the table. Because the session starts before I have access to the authorized user.
    – Hacko
    2 days ago
















If you have a look at config/session.php, you'll notice 'connection' => null and 'table' => 'sessions'; those will have to be updated on the fly by your guards to connect to the correct connection, and/or the correct table based on your logged in user. I'm not sure if there's a way to do that out of the box, but Laravel does note that you can set them on the fly: laravel.com/docs/5.7/…
– Tim Lewis
Nov 21 at 19:15




If you have a look at config/session.php, you'll notice 'connection' => null and 'table' => 'sessions'; those will have to be updated on the fly by your guards to connect to the correct connection, and/or the correct table based on your logged in user. I'm not sure if there's a way to do that out of the box, but Laravel does note that you can set them on the fly: laravel.com/docs/5.7/…
– Tim Lewis
Nov 21 at 19:15












But I do not know, where should I change the table. Because the session starts before I have access to the authorized user.
– Hacko
2 days ago




But I do not know, where should I change the table. Because the session starts before I have access to the authorized user.
– Hacko
2 days ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote













I tried this. This working, but it exists a default session driver, so a user has a session in the default table and in the admin_sessions table.



class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
Administrator::class => UserPolicy::class,
];

public function boot()
{
$this->registerPolicies();

Auth::extend('admin_session', function ($app, $name, array $config) {

$provider = Auth::createUserProvider($config['provider']);
$guard = new SessionGuard($name, $provider, $app->session->driver('admin_database'));

if (method_exists($guard, 'setCookieJar')) {
$guard->setCookieJar($this->app['cookie']);
}

if (method_exists($guard, 'setDispatcher')) {
$guard->setDispatcher($this->app['events']);
}

if (method_exists($guard, 'setRequest')) {
$guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
}

return $guard;
}
}


class SessionServiceProvider extends ServiceProvider
{

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
}


/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
$this->app->session->extend('admin_database', function ($app) {

$table = 'administrator_sessions';

// This is not workin, because I don't have the user her.
/*if (auth()->user() instanceof Customer) {
$table = 'customer_sessions';
}
else if (auth()->user() instanceof Administrator){
$table = 'administrator_sessions';
}*/

$lifetime = $app['config']['session.lifetime'];
$connection = $app['db']->connection($app['config']['session.connection']);

return new DatabaseSessionHandler($connection, $table, $lifetime, $app);
});
}

}





share|improve this answer








New contributor




Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Hacko is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418791%2fmulti-session-tables-in-laravel%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I tried this. This working, but it exists a default session driver, so a user has a session in the default table and in the admin_sessions table.



    class AuthServiceProvider extends ServiceProvider
    {
    protected $policies = [
    Administrator::class => UserPolicy::class,
    ];

    public function boot()
    {
    $this->registerPolicies();

    Auth::extend('admin_session', function ($app, $name, array $config) {

    $provider = Auth::createUserProvider($config['provider']);
    $guard = new SessionGuard($name, $provider, $app->session->driver('admin_database'));

    if (method_exists($guard, 'setCookieJar')) {
    $guard->setCookieJar($this->app['cookie']);
    }

    if (method_exists($guard, 'setDispatcher')) {
    $guard->setDispatcher($this->app['events']);
    }

    if (method_exists($guard, 'setRequest')) {
    $guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
    }

    return $guard;
    }
    }


    class SessionServiceProvider extends ServiceProvider
    {

    /**
    * Register the service provider.
    *
    * @return void
    */
    public function register()
    {
    }


    /**
    * Perform post-registration booting of services.
    *
    * @return void
    */
    public function boot()
    {
    $this->app->session->extend('admin_database', function ($app) {

    $table = 'administrator_sessions';

    // This is not workin, because I don't have the user her.
    /*if (auth()->user() instanceof Customer) {
    $table = 'customer_sessions';
    }
    else if (auth()->user() instanceof Administrator){
    $table = 'administrator_sessions';
    }*/

    $lifetime = $app['config']['session.lifetime'];
    $connection = $app['db']->connection($app['config']['session.connection']);

    return new DatabaseSessionHandler($connection, $table, $lifetime, $app);
    });
    }

    }





    share|improve this answer








    New contributor




    Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote













      I tried this. This working, but it exists a default session driver, so a user has a session in the default table and in the admin_sessions table.



      class AuthServiceProvider extends ServiceProvider
      {
      protected $policies = [
      Administrator::class => UserPolicy::class,
      ];

      public function boot()
      {
      $this->registerPolicies();

      Auth::extend('admin_session', function ($app, $name, array $config) {

      $provider = Auth::createUserProvider($config['provider']);
      $guard = new SessionGuard($name, $provider, $app->session->driver('admin_database'));

      if (method_exists($guard, 'setCookieJar')) {
      $guard->setCookieJar($this->app['cookie']);
      }

      if (method_exists($guard, 'setDispatcher')) {
      $guard->setDispatcher($this->app['events']);
      }

      if (method_exists($guard, 'setRequest')) {
      $guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
      }

      return $guard;
      }
      }


      class SessionServiceProvider extends ServiceProvider
      {

      /**
      * Register the service provider.
      *
      * @return void
      */
      public function register()
      {
      }


      /**
      * Perform post-registration booting of services.
      *
      * @return void
      */
      public function boot()
      {
      $this->app->session->extend('admin_database', function ($app) {

      $table = 'administrator_sessions';

      // This is not workin, because I don't have the user her.
      /*if (auth()->user() instanceof Customer) {
      $table = 'customer_sessions';
      }
      else if (auth()->user() instanceof Administrator){
      $table = 'administrator_sessions';
      }*/

      $lifetime = $app['config']['session.lifetime'];
      $connection = $app['db']->connection($app['config']['session.connection']);

      return new DatabaseSessionHandler($connection, $table, $lifetime, $app);
      });
      }

      }





      share|improve this answer








      New contributor




      Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















        up vote
        0
        down vote










        up vote
        0
        down vote









        I tried this. This working, but it exists a default session driver, so a user has a session in the default table and in the admin_sessions table.



        class AuthServiceProvider extends ServiceProvider
        {
        protected $policies = [
        Administrator::class => UserPolicy::class,
        ];

        public function boot()
        {
        $this->registerPolicies();

        Auth::extend('admin_session', function ($app, $name, array $config) {

        $provider = Auth::createUserProvider($config['provider']);
        $guard = new SessionGuard($name, $provider, $app->session->driver('admin_database'));

        if (method_exists($guard, 'setCookieJar')) {
        $guard->setCookieJar($this->app['cookie']);
        }

        if (method_exists($guard, 'setDispatcher')) {
        $guard->setDispatcher($this->app['events']);
        }

        if (method_exists($guard, 'setRequest')) {
        $guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
        }

        return $guard;
        }
        }


        class SessionServiceProvider extends ServiceProvider
        {

        /**
        * Register the service provider.
        *
        * @return void
        */
        public function register()
        {
        }


        /**
        * Perform post-registration booting of services.
        *
        * @return void
        */
        public function boot()
        {
        $this->app->session->extend('admin_database', function ($app) {

        $table = 'administrator_sessions';

        // This is not workin, because I don't have the user her.
        /*if (auth()->user() instanceof Customer) {
        $table = 'customer_sessions';
        }
        else if (auth()->user() instanceof Administrator){
        $table = 'administrator_sessions';
        }*/

        $lifetime = $app['config']['session.lifetime'];
        $connection = $app['db']->connection($app['config']['session.connection']);

        return new DatabaseSessionHandler($connection, $table, $lifetime, $app);
        });
        }

        }





        share|improve this answer








        New contributor




        Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        I tried this. This working, but it exists a default session driver, so a user has a session in the default table and in the admin_sessions table.



        class AuthServiceProvider extends ServiceProvider
        {
        protected $policies = [
        Administrator::class => UserPolicy::class,
        ];

        public function boot()
        {
        $this->registerPolicies();

        Auth::extend('admin_session', function ($app, $name, array $config) {

        $provider = Auth::createUserProvider($config['provider']);
        $guard = new SessionGuard($name, $provider, $app->session->driver('admin_database'));

        if (method_exists($guard, 'setCookieJar')) {
        $guard->setCookieJar($this->app['cookie']);
        }

        if (method_exists($guard, 'setDispatcher')) {
        $guard->setDispatcher($this->app['events']);
        }

        if (method_exists($guard, 'setRequest')) {
        $guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
        }

        return $guard;
        }
        }


        class SessionServiceProvider extends ServiceProvider
        {

        /**
        * Register the service provider.
        *
        * @return void
        */
        public function register()
        {
        }


        /**
        * Perform post-registration booting of services.
        *
        * @return void
        */
        public function boot()
        {
        $this->app->session->extend('admin_database', function ($app) {

        $table = 'administrator_sessions';

        // This is not workin, because I don't have the user her.
        /*if (auth()->user() instanceof Customer) {
        $table = 'customer_sessions';
        }
        else if (auth()->user() instanceof Administrator){
        $table = 'administrator_sessions';
        }*/

        $lifetime = $app['config']['session.lifetime'];
        $connection = $app['db']->connection($app['config']['session.connection']);

        return new DatabaseSessionHandler($connection, $table, $lifetime, $app);
        });
        }

        }






        share|improve this answer








        New contributor




        Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered yesterday









        Hacko

        11




        11




        New contributor




        Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Hacko is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






















            Hacko is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            Hacko is a new contributor. Be nice, and check out our Code of Conduct.













            Hacko is a new contributor. Be nice, and check out our Code of Conduct.












            Hacko is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418791%2fmulti-session-tables-in-laravel%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            How to ignore python UserWarning in pytest?

            Alexandru Averescu