获取ID为1的设置? Laravel5.4 *

时间:2017-07-24 15:24:44

标签: laravel eloquent

我有以下表格:

使用表格:

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 ='设置';

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?

的设置

非常感谢

2 个答案:

答案 0 :(得分:1)

如果您只想获取指定用户和指定类别的设置,请使用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()而不是networkIsAvailable (Observable<Boolean>)

答案 1 :(得分:1)

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

 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');
}

我的用户模型中仍然存在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;

}