In all of my views I am already able to access {{Auth::user()->name}}
by default, I am trying to add the ability to access {{Profile::user()->...}}
in my views as well but I am having some trouble. I don't really want to use view composers if I dont have to as this post suggests because it looks like I will need to list each view manually. Instead I opted in to use the AppServiceProvider boot method listed in the docs. The problem is I am still not able to call {{ Profile::user()->title }}
for example. I am getting the following error:
ErrorException in AppServiceProvider.php line 19:
Non-static method App\Profile::user() should not be called statically, assuming $this from incompatible context
Here is my AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use View;
use App\Profile;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
View::share('user', Profile::user());
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
Here is my model Profile.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
//
protected $fillable = [
'bio',
'linkedin_url',
'facebook_url',
'twitter_username',
'title',
'profile_image',
'user_id',
];
public function user()
{
return $this->belongsTo('user');
}
}
What am I doing wrong? How can I have access to users profile data in all views? Can I see an example?