【问题标题】:Get Settings with id of 1? Laravel5.4*获取 id 为 1 的设置? Laravel5.4*
【发布时间】:2017-12-30 06:23:41
【问题描述】:

我有以下表格:

使用表:

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('firstname');
        $table->string('surename');
        $table->string('address')->nullable();
        $table->string('city')->nullable();
        $table->string('country')->nullable();
        $table->string('postcode')->nullable();
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->string('facebook_id')->nullable();
        $table->string('linkedin_id')->nullable();
        $table->string('twitter_id')->nullable();
        $table->timestamps();
    });

设置表:

 Schema::create('settings', function (Blueprint $table) {
        $table->increments('id');
        $table->string('value');
        $table->timestamps();
    });

Setting_user(数据透视表):

 Schema::create('setting_user', function (Blueprint $table) {
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->integer('setting_id')->unsigned();
        $table->foreign('setting_id')->references('id')->on('settings');
        $table->timestamps();
    });

类别表:

Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });

Setting_category(数据透视表):

 Schema::create('setting_category', function (Blueprint $table) {
        $table->integer('category_id')->unsigned();
        $table->foreign('category_id')->references('id')->on('categories');
        $table->integer('setting_id')->unsigned();
        $table->foreign('setting_id')->references('id')->on('settings');
        $table->timestamps();
    });

在我的用户模型中,我有一个 belongsToMany 关系:

public function settings()
{
    return $this->belongsToMany(Setting::class, 'setting_user', 'user_id','setting_id')->with('category');
}

在我的设置模型中:

受保护的 $table = 'settings';

public function category()
{
    return $this->hasMany(Categories::class,'id');
}

当我这样访问它时:

public function settings()
{
    $user = User::find(1);
    return $user->settings()->with('category')->first();
}

这是我得到的结果:

{"id":1,"value":"87658675.jpg","created_at":"2017-07-04 00:00:00","updated_at":null,"pivot":{"user_id":1,"setting_id":1},"category":[{"id":1,"name":"avatar","created_at":"2017-07-06 00:00:00","updated_at":null}]}

如何只获取 category->id == 1 的设置?

非常感谢

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    如果您只想获取指定用户和指定类别的设置,请使用whereHas()

    public function settings()
    {
        $categoryId = 1;
        $userId = 1;
    
        return Settings::whereHas('categories', function ($q) use ($categoryId) {
                $q->where('id', $categoryId);
            })
            ->whereHas('users', function ($q) use ($userId) {
                $q->where('id', $userId);
            })
            ->get();
    }
    

    要完成这项工作,请修复您的人际关系。看起来所有关系都应该是belongsToMany() 而不是hasMany()

    【讨论】:

    • 感谢您的提示。请参阅上面的解决方案 :) @Alexey Mezenin
    • 我确实投票给了你的答案。但我不知道如何选择最佳答案。我很高兴为你做这件事:)
    【解决方案2】:

    我已将设置模型中的关系更改为:

     public function users()
    {
        return $this->belongsToMany(User::class, 'setting_user','setting_id', 'user_id');
    }
    
    public function categories()
    {
        return $this->belongsToMany(Categories::class,'setting_category','category_id','setting_id');
    }
    

    我的 User 模型中仍然有 belongsToMany 关系:

     public function settings()
    {
        return $this->belongsToMany(Setting::class, 'setting_user', 'user_id','setting_id');
    }
    

    我创建了另一种方法来提取 ID 为“1”的头像,如下所示:

    /**
    * Get user's avatar.
    */
    public function getAvatarAttribute() {
    
        $categoryId = 1;
        $userId = Auth::user()->id;
    
        $avatar = $this->settings()->whereHas('categories', function ($q) use ($categoryId) {
            $q->where('id', $categoryId);
        })->whereHas('users', function ($q) use ($userId) {
            $q->where('id', $userId);
        })->first();
    
        if(!$avatar)
        {
            return "default-avatar.jpg"; 
        }
    
        return $avatar->value;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-02-24
      • 1970-01-01
      • 2011-11-10
      • 1970-01-01
      • 2014-11-19
      • 2020-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多