Php 如何从3个模型中获取数据,这3个模型与一个模型和多个模型之间存在关联?

Php 如何从3个模型中获取数据,这3个模型与一个模型和多个模型之间存在关联?,php,mysql,laravel,orm,eloquent,Php,Mysql,Laravel,Orm,Eloquent,我的项目中有一个友谊请求模块。其中使用了以下三个表:- 使用者 用户配置文件 友谊 用户:-Id、slug、姓名、电子邮件、密码 UserProfile:-Id、user\u slug、Profile\u pic、DOB..等等 友谊:-Id、用户号、好友号、状态 关系:- public function Profile(){ return $this->hasOne('UserProfile','user_slug','slug')->first(); } public

我的项目中有一个友谊请求模块。其中使用了以下三个表:-

  • 使用者
  • 用户配置文件
  • 友谊
  • 用户:-Id、slug、姓名、电子邮件、密码

    UserProfile:-Id、user\u slug、Profile\u pic、DOB..等等

    友谊:-Id、用户号、好友号、状态

    关系:-

    public function Profile(){
        return $this->hasOne('UserProfile','user_slug','slug')->first();
    }
    
    public function sentFriendshipRequests(){
        return $this->hasMany('Friendship','user_slug','slug');
    }
    
    public function receivedFriendshipRequests(){
        return $this->hasMany('Friendship','friend_slug','slug');
    }   
    
    public function User(){
        return $this->belongsTo('User','user_slug','slug');
    }
    
    public function receiver(){
        return $this->belongsTo('User','friend_slug','slug');
    }
    
    
    public function sender(){
        return $this->belongsTo('User','user_slug','slug');
    }
    
    $friendship_requests= Auth::user()->receivedFriendshipRequests();
    
    foreach($friendship_requests as $frnd_req)
    {
        $sender_user=User::where('slug',$frnd_req->user_slug());
    }
    
    用户模型:-

    public function Profile(){
        return $this->hasOne('UserProfile','user_slug','slug')->first();
    }
    
    public function sentFriendshipRequests(){
        return $this->hasMany('Friendship','user_slug','slug');
    }
    
    public function receivedFriendshipRequests(){
        return $this->hasMany('Friendship','friend_slug','slug');
    }   
    
    public function User(){
        return $this->belongsTo('User','user_slug','slug');
    }
    
    public function receiver(){
        return $this->belongsTo('User','friend_slug','slug');
    }
    
    
    public function sender(){
        return $this->belongsTo('User','user_slug','slug');
    }
    
    $friendship_requests= Auth::user()->receivedFriendshipRequests();
    
    foreach($friendship_requests as $frnd_req)
    {
        $sender_user=User::where('slug',$frnd_req->user_slug());
    }
    
    用户配置文件模型:-

    public function Profile(){
        return $this->hasOne('UserProfile','user_slug','slug')->first();
    }
    
    public function sentFriendshipRequests(){
        return $this->hasMany('Friendship','user_slug','slug');
    }
    
    public function receivedFriendshipRequests(){
        return $this->hasMany('Friendship','friend_slug','slug');
    }   
    
    public function User(){
        return $this->belongsTo('User','user_slug','slug');
    }
    
    public function receiver(){
        return $this->belongsTo('User','friend_slug','slug');
    }
    
    
    public function sender(){
        return $this->belongsTo('User','user_slug','slug');
    }
    
    $friendship_requests= Auth::user()->receivedFriendshipRequests();
    
    foreach($friendship_requests as $frnd_req)
    {
        $sender_user=User::where('slug',$frnd_req->user_slug());
    }
    
    友谊模式:-

    public function Profile(){
        return $this->hasOne('UserProfile','user_slug','slug')->first();
    }
    
    public function sentFriendshipRequests(){
        return $this->hasMany('Friendship','user_slug','slug');
    }
    
    public function receivedFriendshipRequests(){
        return $this->hasMany('Friendship','friend_slug','slug');
    }   
    
    public function User(){
        return $this->belongsTo('User','user_slug','slug');
    }
    
    public function receiver(){
        return $this->belongsTo('User','friend_slug','slug');
    }
    
    
    public function sender(){
        return $this->belongsTo('User','user_slug','slug');
    }
    
    $friendship_requests= Auth::user()->receivedFriendshipRequests();
    
    foreach($friendship_requests as $frnd_req)
    {
        $sender_user=User::where('slug',$frnd_req->user_slug());
    }
    
    目标:-我想显示用户收到的待定友谊请求列表

    所需数据:- 当前记录的用户名、Slug、友谊请求发送者的个人资料图片的所有处于挂起状态的友谊请求

    我的方法:-

    public function Profile(){
        return $this->hasOne('UserProfile','user_slug','slug')->first();
    }
    
    public function sentFriendshipRequests(){
        return $this->hasMany('Friendship','user_slug','slug');
    }
    
    public function receivedFriendshipRequests(){
        return $this->hasMany('Friendship','friend_slug','slug');
    }   
    
    public function User(){
        return $this->belongsTo('User','user_slug','slug');
    }
    
    public function receiver(){
        return $this->belongsTo('User','friend_slug','slug');
    }
    
    
    public function sender(){
        return $this->belongsTo('User','user_slug','slug');
    }
    
    $friendship_requests= Auth::user()->receivedFriendshipRequests();
    
    foreach($friendship_requests as $frnd_req)
    {
        $sender_user=User::where('slug',$frnd_req->user_slug());
    }
    
    有没有其他合适的方法可以通过使用雄辩的关系方法而不使用连接来获取这些数据。我的意思是如何在一个查询中使用HasOne和HasMany关系获取数据

    非常感谢您的帮助或建议


    谢谢

    这是一个自引用的多对多关系,因此您根本不需要那些
    有许多
    /
    属于
    关系。
    您只需将一个
    belongToMany
    用于自己的请求,另一个用于接收的请求

    请先阅读以下内容:

    然后添加以下关系:

    // pending requests of mine
    function pendingFriendsOfMine()
    {
      return $this->belongsToMany('User', 'friendship', 'user_slug', 'friend_slug')
         ->wherePivot('accepted', '=', 0)
         ->withPivot('accepted');
    }
    
    // pending received requests
    function pendingFriendOf()
    {
      return $this->belongsToMany('User', 'friendship', 'friend_slug', 'user_slug')
         ->wherePivot('accepted', '=', 0)
         ->withPivot('accepted');
    }
    
    // accessor allowing you call $user->friends
    public function getPendingFriendsAttribute()
    {
        if ( ! array_key_exists('pendingFriends', $this->relations)) $this->loadPendingFriends();
    
        return $this->getRelation('pendingFriends');
    }
    
    protected function loadPendingFriends()
    {
        if ( ! array_key_exists('pendingFriends', $this->relations))
        {
            $pending = $this->mergePendingFriends();
    
            $this->setRelation('pendingFriends', $pending);
        }
    }
    
    protected function mergePendingFriends()
    {
        return $this->pendingFriendsOfMine->merge($this->pendingFriendOf);
    }
    
    然后,您只需使用嵌套关系加载它:

    $user = Auth::user();
    $user->load('pendingFriendsOfMine.profile', 'pendingFriendOf.profile');
    // the above will execute 4 queries - 2 for requests, 2 for related profiles
    
    $pendingFriends = $user->pendingFriends; // for all pending requests
    // or
    // $user->load('pendingFriendOf.profile'); // 2 queries in this case
    // $pendingRequests = $user()->pendingFriendOf; // for received requests only
    
    foreach ($pendingFriends as $user) {
      $user->profile; // eager loaded profie model
    }
    

    此外,这里还有一些代码中的错误:

    // there can't be first() in the relation definition
    // and it is not needed anyway
    public function Profile(){
        return $this->hasOne('UserProfile','user_slug','slug')->first();
    }
    
    
    
    // You never want to run this User::where() ...
    // in a foreach loop, for it will result in n+1 queries issue
    // You need eager loading instead.
    foreach($friendship_requests as $frnd_req)
    {
        $sender_user=User::where('slug',$frnd_req->user_slug());
    }
    

    谢谢你详细的回答。我将尝试在我的应用程序中实现这个逻辑。脱帽人:)谢谢你的帮助。。。。我遵循你的方法,但我没有得到待决朋友的细节。它正在返回null<代码>$user=Auth::user()$用户->加载('pendingFriendsOfMine.profile','PendingFriendsOf.profile')$pendingFriends=$user->pendingFriends;foreach($pendingFriends as$user){var_dump($user->profile);}'在调用
    $pendingFriends=$user->pendingFriends()方法之前,我是否应该调用
    $user->loadPendingFriends()`方法
    pendingFriendsOfMine
    获取随机用户,并检查他是否有
    个人资料
    。对于那些没有相关配置文件的用户,它应该返回
    null
    。顺便说一句,我修正了FK打字错误。谢谢你的帮助。我正在使用以下代码:-
    $user=Auth::user();$user->load('pendingFriendOf.Profile');$friendshipRequests=$user->pendingFriendOf
    但现在我如何才能获取发送友谊请求的用户的友谊Id和个人资料详细信息?1为什么需要该友谊Id?这只是一张透视表
    pendingFriendOf
    是连接了
    pivot
    对象的
    User
    模型的集合,因此您可以调用
    ->pivot->id
    (但是您需要在关系定义中使用
    withPivot('id')
    。2配置文件详细信息的访问方式与我的示例类似。这一切都很有效,因此请确保您没有弄糟什么。