【问题标题】:How can I get the commnunity followers by using laravel Eloquent relationship如何使用 laravel Eloquent 关系获得社区关注者
【发布时间】:2019-04-07 09:26:20
【问题描述】:

我正在尝试使用Laravel eloquent关系创建社区关注系统,我无法解决问题,请帮助

基本上,我正在尝试创建基于社区(例如:商业与专业、健康与保健、科学与技术等)的活动系统。

它给了我以下错误

Illuminate\Database\QueryException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'acp_db.community_users' doesn't exist (SQL: select * from `community_users` where `community_users`.`id` = 8 limit 1) in file /Users/muhammadowais/mowais/academics-provider/website/working/acpapi/vendor/laravel/framework/src/Illuminate/Database/Connection.php on line 664

为了通过 Id 获得社区的关注者,我创建了以下表格

1) 用户

2) event_categories(可以说是社区)

3) community_user (user_id, community_id)

控制器

public function communityBySlug($slug){
        $eventCategory = EventCategories::where(['slug' => $slug])->first();
        $eventCategoryId = $eventCategory->id;


        // Getting users by community id
        $users = CommunityUsers::find(8)->users();

        return Response::json(
            [
                'data' => $eventCategory,
                'community_followers' => $users
            ]
        );
    }

模型:社区用户

class CommunityUsers extends Model
{
    protected $fillable = ['community_id', 'user_id'];
    protected $guarded = [];

    public function Users(){
        return $this->belongsToMany(User::class, 'users');
    }
}

【问题讨论】:

    标签: laravel laravel-5 eloquent-relationship


    【解决方案1】:

    假设社区用户是映射多对多关系表的模型,您应该在数据库中为该模型指定正确的表名。

    class CommunityUsers extends Model
    {
        /**
         * The table associated with the model.
         *
         * @var string
         */
        protected $table = 'community_users';
    }
    

    另外,请记住,Eloquent 不支持复合主键,因此您必须在CommunityUsers 模型在其上使用 find() 方法,否则 Laravel 将按 id 列搜索。

    我宁愿在关系表中插入一个新的主自动增量列,并使用如下过滤器检索特定社区:

    CommunityUsers::where('community_id', $id)->first();
    

    注意:您也可以将该过滤器设为CommunityUsers 范围方法。

    此外,请注意您从UsersCommunityUsers 的关系是一对多 关系(一个User 映射到许多CommunityUsers 对([community_id, user_id]))

    重新思考关系映射

    如果考虑这三个表,可以将其建模为UsersCommunities 之间的多对多 关系。

    关系应该是:

    模型:用户

    class User extends Authenticatable
    {
        public function communities()
        {
            return $this->belongsToMany(EventCategories::class, 'community_user', 'user_id', 'community_id');
        }
    }
    

    模型:EventCategories(假设这是您的社区模型)

    class EventCategories extends Model
    {
        public function users()
        {
            return $this->belongsToMany(User::class, 'community_user', 'community_id');
        }
    }
    

    注意:上面的代码可能需要根据您的模型及其表定义进行一些调整。

    在关系定义之后,您可以直接在EventCategories 模型上使用它:

    public function communityBySlug($slug){
        $eventCategory = EventCategories::with('users')
            ->whereSlug($slug)
            ->first();
    
        return Response::json(
            [
                'data' => $eventCategory,
                'community_followers' => $eventCategory->users
            ]
        );
    }
    

    【讨论】:

      【解决方案2】:

      假设community_id 是您的CommunityUsers 表中的主键,那么问题出在您的Users() 函数中:

      public function Users(){
          return $this->belongsToMany(User::class, 'users');
      }
      

      belongsToMany的第二个参数应该是外键,也就是user_id

      【讨论】:

      • 我这样做了return $this->belongsToMany(User::class, 'users', 'user_id'); 它给了我这个错误SQLSTATE[42S02]: Base table or view not found: 1146 Table 'acp_db.community_users' doesn't exist (SQL: select * from `community_users` where `community_users`.`id` = 8 limit 1)
      猜你喜欢
      • 2020-04-04
      • 2020-06-19
      • 1970-01-01
      • 2014-10-29
      • 2021-11-06
      • 1970-01-01
      • 2021-10-29
      • 2011-08-23
      • 1970-01-01
      相关资源
      最近更新 更多