<i id='JgYQE'><tr id='JgYQE'><dt id='JgYQE'><q id='JgYQE'><span id='JgYQE'><b id='JgYQE'><form id='JgYQE'><ins id='JgYQE'></ins><ul id='JgYQE'></ul><sub id='JgYQE'></sub></form><legend id='JgYQE'></legend><bdo id='JgYQE'><pre id='JgYQE'><center id='JgYQE'></center></pre></bdo></b><th id='JgYQE'></th></span></q></dt></tr></i><div id='JgYQE'><tfoot id='JgYQE'></tfoot><dl id='JgYQE'><fieldset id='JgYQE'></fieldset></dl></div>
<tfoot id='JgYQE'></tfoot>
    <bdo id='JgYQE'></bdo><ul id='JgYQE'></ul>
  • <small id='JgYQE'></small><noframes id='JgYQE'>

  • <legend id='JgYQE'><style id='JgYQE'><dir id='JgYQE'><q id='JgYQE'></q></dir></style></legend>

        与 Laravel 的友谊系统:多对多关系

        时间:2023-09-24
          <bdo id='ASbLf'></bdo><ul id='ASbLf'></ul>
            <tbody id='ASbLf'></tbody>

              <small id='ASbLf'></small><noframes id='ASbLf'>

                1. <tfoot id='ASbLf'></tfoot>
                  <i id='ASbLf'><tr id='ASbLf'><dt id='ASbLf'><q id='ASbLf'><span id='ASbLf'><b id='ASbLf'><form id='ASbLf'><ins id='ASbLf'></ins><ul id='ASbLf'></ul><sub id='ASbLf'></sub></form><legend id='ASbLf'></legend><bdo id='ASbLf'><pre id='ASbLf'><center id='ASbLf'></center></pre></bdo></b><th id='ASbLf'></th></span></q></dt></tr></i><div id='ASbLf'><tfoot id='ASbLf'></tfoot><dl id='ASbLf'><fieldset id='ASbLf'></fieldset></dl></div>
                  <legend id='ASbLf'><style id='ASbLf'><dir id='ASbLf'><q id='ASbLf'></q></dir></style></legend>
                  本文介绍了与 Laravel 的友谊系统:多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Laravel 创建一个友谊系统(我是从它开始的),但我被关系阻止了.事情是这样的:有一张表 Users 和一张表 Friends ,其中包含以下列:

                  I'm trying to create a Friendship system with Laravel (I'm starting with it) but I'm blocked with relationships. Here's the thing : there is one table Users and one table Friends which contains the following columns :

                  friends: id, user_id, friend_id, accepted.
                  

                  它看起来像多对多,所以这是我在用户类上设置的:

                  It looks like a Many to Many so here's what I set on User class :

                  class User extends Eloquent {
                      function friends()
                      {
                          return $this->belongsToMany('User');
                      }
                  }
                  

                  但是当我尝试:

                  $friends = User::find($id)->friends()->get()
                  

                  我有这个错误:

                  Base table or view not found: 1146 Table 'base.user_user' doesn't exist
                  

                  我想获取一个用户的好友列表,无论用户是发送邀请还是收到邀请.因此,用户可以根据 user_id 或friend_id 获取数据,然后根据该列检索其他用户的数据.

                  I would like to get a list of the Friends of a user, no matters if the user sent the invitation or received it. So the user can ba on user_id or on friend_id and then I retrieve the data of the other user depending of that column.

                  有什么想法吗?谢谢!

                  这是我使用的代码:

                  $usersWithFriends = User::with('friendsOfMine', 'friendOf')->get();
                  $user = User::find(Auth::id())->friends;
                  
                  foreach($user as $item) {
                      echo $item->first()->pivot->accepted;
                  } 
                  

                  推荐答案

                  tldr;您需要 2 个反向关系才能使其工作,请检查下面的 SETUP 和 USAGE

                  <小时>

                  首先是错误 - 这就是你的关系应该是什么样子:

                  tldr; you need 2 inverted relationships to make it work, check SETUP and USAGE below


                  First off the error - this is how your relation should look like:

                  function friends()
                  {
                    return $this->belongsToMany('User', 'friends', 'user_id', 'friend_id')
                      // if you want to rely on accepted field, then add this:
                      ->wherePivot('accepted', '=', 1);
                  }
                  

                  然后它就会正常工作:

                  $user->friends; // collection of User models, returns the same as:
                  $user->friends()->get();
                  

                  <小时>

                  设置

                  但是您希望关系以两种方式工作.Eloquent 不提供这种关系,因此您可以改为使用 2 个反向关系并合并结果:


                  SETUP

                  However you would like the relation to work in both ways. Eloquent doesn't provide a relation of that kind, so you can instead use 2 inverted relationships and merge the results:

                  // friendship that I started
                  function friendsOfMine()
                  {
                    return $this->belongsToMany('User', 'friends', 'user_id', 'friend_id')
                       ->wherePivot('accepted', '=', 1) // to filter only accepted
                       ->withPivot('accepted'); // or to fetch accepted value
                  }
                  
                  // friendship that I was invited to 
                  function friendOf()
                  {
                    return $this->belongsToMany('User', 'friends', 'friend_id', 'user_id')
                       ->wherePivot('accepted', '=', 1)
                       ->withPivot('accepted');
                  }
                  
                  // accessor allowing you call $user->friends
                  public function getFriendsAttribute()
                  {
                      if ( ! array_key_exists('friends', $this->relations)) $this->loadFriends();
                  
                      return $this->getRelation('friends');
                  }
                  
                  protected function loadFriends()
                  {
                      if ( ! array_key_exists('friends', $this->relations))
                      {
                          $friends = $this->mergeFriends();
                  
                          $this->setRelation('friends', $friends);
                      }
                  }
                  
                  protected function mergeFriends()
                  {
                      return $this->friendsOfMine->merge($this->friendOf);
                  }
                  

                  用法

                  通过这样的设置,您可以做到:

                  USAGE

                  With such setup you can do this:

                  // access all friends
                  $user->friends; // collection of unique User model instances
                  
                  // access friends a user invited
                  $user->friendsOfMine; // collection
                  
                  // access friends that a user was invited by
                  $user->friendOf; // collection
                  
                  // and eager load all friends with 2 queries
                  $usersWithFriends = User::with('friendsOfMine', 'friendOf')->get();
                  
                  // then
                  $users->first()->friends; // collection
                  
                  // Check the accepted value:
                  $user->friends->first()->pivot->accepted;
                  

                  这篇关于与 Laravel 的友谊系统:多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel Eloquent Query 使用 WHERE 和 OR AND OR? 下一篇:如何在 Laravel Eloquent 查询(或使用查询生成器)中为

                  相关文章

                  最新文章

                2. <legend id='6BEal'><style id='6BEal'><dir id='6BEal'><q id='6BEal'></q></dir></style></legend>
                  <i id='6BEal'><tr id='6BEal'><dt id='6BEal'><q id='6BEal'><span id='6BEal'><b id='6BEal'><form id='6BEal'><ins id='6BEal'></ins><ul id='6BEal'></ul><sub id='6BEal'></sub></form><legend id='6BEal'></legend><bdo id='6BEal'><pre id='6BEal'><center id='6BEal'></center></pre></bdo></b><th id='6BEal'></th></span></q></dt></tr></i><div id='6BEal'><tfoot id='6BEal'></tfoot><dl id='6BEal'><fieldset id='6BEal'></fieldset></dl></div>
                      • <bdo id='6BEal'></bdo><ul id='6BEal'></ul>

                      <small id='6BEal'></small><noframes id='6BEal'>

                      <tfoot id='6BEal'></tfoot>