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

EventFellows's avatar

How to bind additional user relationship to vue instance in spark?

Let's assume a user (from users table) is part of a company (from companies table) and I would like to bind the company info to the vue instance so I can access it in blade views. In backend I can do

$user->company();

I would essentially want to do the same in the frontend.

Spark documentation says something about (in https://spark.laravel.com/docs/1.0/customization ->current user):

For example, you may wish to load extra relationships on the User instance before it is returned. You may do so by swapping the UserRepository@current method.

When looking at the method how it is setup, it looks like this:

    public function current()
    {
        if (Auth::check()) {
            return $this->find(Auth::id())->shouldHaveSelfVisibility();
        }
    }

How would I integrate / setup any additional relationship here?

0 likes
2 replies
spar_x's avatar
spar_x
Best Answer
Level 2

I know it's been a long time but I just had to do the same thing and I agree the docs could have been a little clearer but anyway wasn't hard to get this to work:

In the SparkServiceProvider at the bottom of the boot function

        Spark::swap('UserRepository@current', function () {
            if (Auth::check()) {
                $user = $this->find(Auth::id())->shouldHaveSelfVisibility();
                $user->load('profile');
                return $user;
            }
        });

In my case I am loading a relationship profile -> hasOne

I tried using ->with() but that didn't work with the repository

1 like

Please or to participate in this conversation.