【问题标题】:Laravel access data from all viewsLaravel 从所有视图访问数据
【发布时间】: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


【解决方案1】:

@Saumya 和@martindilling 都是正确的;您正在绑定到您正在使用的服务提供商中的 $user 变量。您只需将 user() 方法设为静态即可以您想要的方式访问它:

public static function user(){
    if (auth()->user()) return auth()->user()->profile;
}

如果你使用它,那么如果你不想要它,你就不需要 View 绑定。您可以在任何 Blade 模板中使用 {{ Profile::user() }}。但这也有点傻,因为现在你已经覆盖了你的关系方法名称。

如果您真的想超级轻松地访问当前用户的个人资料,为什么不创建一个可以从任何地方访问的辅助方法呢? currentUserProfile() 还是什么?我不介意为每个项目保留一个包含一些快捷方法的文件,但是在向其中添加内容时必须有良好的纪律,否则您的代码将很快变得不可读。

我发现第二个答案here 是添加您自己的自定义辅助方法的一种非常清晰的方法。

【讨论】:

  • 也是一个很好的答案,我有其他一切都在处理这种关系,我只是不知道在我看来它就像调用auth()-&gt;user()-&gt;profile 一样简单(没有意识到它是附加到用户并认为我需要一个作曲家视图、帮助函数或服务提供者来完成这个)但我也从你的回答中学到了,所以谢谢你
【解决方案2】:

您根本不需要共享此变量,因为您只想获取用户的个人资料(您总是加载用户数据)并且它是一对一的关系。只需添加适当的关系:

public function profile()
{
    return $this->hasOne('\App\Profile');
}

然后在应用程序的任何地方使用它:

auth()->user()->profile->city

如果您要共享变量,您将向所有视图添加第二个查询,而在实际应用中您绝对不想这样做。

【讨论】:

  • 这是我需要的东西,非常感谢
【解决方案3】:

当您使用View::share('user', Profile::user()); 时,您的个人资料用户将在视图中显示为{{ $user }} :)

是的,您收到的错误消息是由于 Saumya Rastogi 所写的原因,您需要获取特定个人资料的用户 :)

【讨论】:

    【解决方案4】:

    根据您的代码,您正在静态调用非静态方法。你不应该那样做。相反,您应该使用一个配置文件,您需要这样的特定用户:

    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', auth()-&gt;user-&gt;profile); 时,出现以下错误:Undefined property: Illuminate\Auth\AuthManager::$user
    • 谢谢,现在我收到以下错误:Trying to get property of non-object 我假设它是View::share('user_profile', auth()-&gt;user()-&gt;profile);profile 部分我确保我的配置文件模型具有用户方法并且用户模型具有配置文件方法,例如你上面的例子。
    • 根据我的代码一切都很好,请确保您的profile 表有user_id 列并且正确使用和定义了关系!
    猜你喜欢
    • 2020-09-05
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多