Laravel 从所有视图访问数据
Laravel access data from all views
在我的所有视图中,我已经可以默认访问 {{Auth::user()->name}}
,我正在尝试在我的视图中添加访问 {{Profile::user()->...}}
的能力,但我遇到了一些麻烦。如果我不需要像文档中列出的 because it looks like I will need to list each view manually. Instead I opted in to use the 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');
}
}
我做错了什么?我如何才能访问所有视图中的用户配置文件数据?我可以看一个例子吗?
根据您的代码,您正在静态调用非静态方法。你不应该那样做。相反,你应该使用一个配置文件,你想要这样的特定用户:
View::share('user', Profile::find(1)->user);
您还可以获得所有具有这样配置文件的用户,您的模型应该如下所示:
class User extends Model {
public function profile() {
return $this->hasOne(Profile::class);
}
}
class Profile extends Model {
public function user() {
return $this->belongsTo(User::class);
}
}
View::share('user', User::with('profile')->get()); // <----- Get all users with profile
或者如果你想获取认证用户的个人资料,你可以这样做:
View::share('user', auth()->user); // <---- Gets the authenticated User
View::share('user_profile', auth()->user()->profile); // <---- Gets the authenticated User's profile
希望对您有所帮助!
当您使用 View::share('user', Profile::user());
时,您的个人资料用户将在视图中显示为 {{ $user }}
:)
是的,您收到的错误消息是由于 Saumya Rastogi 所写的原因,即您需要获取特定配置文件的用户:)
@Saumya 和@martindilling 都正确;您正在绑定到您正在使用的服务提供商中的 $user
变量。您只需要将 user()
方法设为静态即可按您想要的方式访问它:
public static function user(){
if (auth()->user()) return auth()->user()->profile;
}
如果您使用它,那么如果您不需要视图绑定,则不需要它。您可以在任何 Blade 模板中使用 {{ Profile::user() }}
。但这也有点傻,因为现在你已经覆盖了你的关系方法名称。
如果您真的想要超级轻松地访问当前用户的个人资料,为什么不制作一个可以从任何地方访问的辅助方法呢? currentUserProfile()
之类的?我不介意为每个项目保留一个包含一些快捷方法的文件,但是在向其中添加东西时你必须有良好的纪律,否则你的代码很快就会变得不可读。
我发现第二个答案 here 是添加您自己的自定义辅助方法的一种非常清晰的方法。
您根本不需要共享此变量,因为您只想获取用户的个人资料(您总是加载用户数据)并且它是一对一的关系。只需添加适当的关系:
public function profile()
{
return $this->hasOne('\App\Profile');
}
然后在应用程序的任何地方使用它:
auth()->user()->profile->city
如果您要共享变量,您将向所有视图添加第二个查询,而在实际应用中您绝对不想这样做。
在我的所有视图中,我已经可以默认访问 {{Auth::user()->name}}
,我正在尝试在我的视图中添加访问 {{Profile::user()->...}}
的能力,但我遇到了一些麻烦。如果我不需要像文档中列出的 {{ 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');
}
}
我做错了什么?我如何才能访问所有视图中的用户配置文件数据?我可以看一个例子吗?
根据您的代码,您正在静态调用非静态方法。你不应该那样做。相反,你应该使用一个配置文件,你想要这样的特定用户:
View::share('user', Profile::find(1)->user);
您还可以获得所有具有这样配置文件的用户,您的模型应该如下所示:
class User extends Model {
public function profile() {
return $this->hasOne(Profile::class);
}
}
class Profile extends Model {
public function user() {
return $this->belongsTo(User::class);
}
}
View::share('user', User::with('profile')->get()); // <----- Get all users with profile
或者如果你想获取认证用户的个人资料,你可以这样做:
View::share('user', auth()->user); // <---- Gets the authenticated User
View::share('user_profile', auth()->user()->profile); // <---- Gets the authenticated User's profile
希望对您有所帮助!
当您使用 View::share('user', Profile::user());
时,您的个人资料用户将在视图中显示为 {{ $user }}
:)
是的,您收到的错误消息是由于 Saumya Rastogi 所写的原因,即您需要获取特定配置文件的用户:)
@Saumya 和@martindilling 都正确;您正在绑定到您正在使用的服务提供商中的 $user
变量。您只需要将 user()
方法设为静态即可按您想要的方式访问它:
public static function user(){
if (auth()->user()) return auth()->user()->profile;
}
如果您使用它,那么如果您不需要视图绑定,则不需要它。您可以在任何 Blade 模板中使用 {{ Profile::user() }}
。但这也有点傻,因为现在你已经覆盖了你的关系方法名称。
如果您真的想要超级轻松地访问当前用户的个人资料,为什么不制作一个可以从任何地方访问的辅助方法呢? currentUserProfile()
之类的?我不介意为每个项目保留一个包含一些快捷方法的文件,但是在向其中添加东西时你必须有良好的纪律,否则你的代码很快就会变得不可读。
我发现第二个答案 here 是添加您自己的自定义辅助方法的一种非常清晰的方法。
您根本不需要共享此变量,因为您只想获取用户的个人资料(您总是加载用户数据)并且它是一对一的关系。只需添加适当的关系:
public function profile()
{
return $this->hasOne('\App\Profile');
}
然后在应用程序的任何地方使用它:
auth()->user()->profile->city
如果您要共享变量,您将向所有视图添加第二个查询,而在实际应用中您绝对不想这样做。