Anyone?
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?
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
Please or to participate in this conversation.