【发布时间】:2017-05-07 03:13:11
【问题描述】:
在我的所有视图中,默认情况下我已经能够访问{{Auth::user()->name}},我正在尝试在我的视图中添加访问{{Profile::user()->...}} 的能力,但我遇到了一些麻烦。如果我不需要this post suggests,我真的不想使用视图作曲家,因为看起来我需要手动列出每个视图。相反,我选择使用文档中列出的AppServiceProvider boot method。问题是我仍然无法打电话给{{ Profile::user()->title }}。我收到以下错误:
ErrorException in AppServiceProvider.php line 19:
Non-static method App\Profile::user() should not be called statically, assuming $this from incompatible context
这是我的 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()
{
//
}
}
这是我的模型 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');
}
}
我做错了什么?如何访问所有视图中的用户个人资料数据?我可以看一个例子吗?
【问题讨论】:
-
你想要登录用户的个人资料吗?
-
User模型和Profile有关系吗?然后您可以使用 {{ Auth::user()->profile->myAttribute }}
标签: php laravel laravel-5.3