当前位置: 首页 > 知识库问答 >
问题:

是否获取id为1的设置?Laravel5.4*

杨昆
2023-03-14

我有以下表格:

使用表格:

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

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

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}]}

如何仅获取类别设置-

非常感谢

共有2个答案

广宏远
2023-03-14

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

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

在我的用户模型中,我仍然具有以下关系:

 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;

}
许兴文
2023-03-14

如果只想获取指定用户和指定类别的设置,请使用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();
}

要想成功,修复你的关系。看起来所有的关系都应该是属性。

 类似资料:
  • 问题内容: 我正在我的android设备上测试adMob,并在获取说明文件之后,尝试获取执行adRequest的设备ID。但是,我无法在logCat中找到设备ID!我做错了什么? 编辑:这不是重复的,其他帖子中的那些方法对我不起作用。 问题答案: final TelephonyManager tm =(TelephonyManager)getBaseContext().getSystemServi

  • 问题内容: 如果我能得到类似下面的信息,那将是很棒的。 伪代码: 在打印U时,将返回以下内容: 能够获得小部件设置将非常有用。这样我就可以相应地操纵其他小部件。 问题答案: 如果知道所需的设置,则可以使用该方法获取值,例如 它将打印 如果您想知道所有可用的选项,widget.config包含配置,并且如果您希望的话,可以创建您可能需要的全部或部分设置,例如 输出:

  • 现在与android 10更新的权限和安全性,我们不能访问用户的设备ID和IMEI号码,但我想要一些设备的唯一ID,以便我们可以跟踪用户。 要求是我们想要/限制一部手机的一次登录

  • 问题内容: 我需要检查value是否定义为任何东西,包括null。将null值视为undefined并返回。以以下为例: 请注意,这是未定义的。 我需要找到满足以下条件的条件: 有任何想法吗? 问题答案: IIRC,您可以使用此功能:

  • X1.2新增 sp_get_cmf_settings($key); 功能: 获取cmf的设置;如果key为空则返回所有设置,如果key不为空,则返回相应key的设置 参数: $key:默认为空,设置的key 返回: 如果key为空则返回所有设置,如果key不为空,则返回相应key的设置

  • 问题内容: 我知道如何通过执行以下操作来为名称已经知道的属性创建getter和setter: 现在,我的问题是,是否可以定义像这样的所有获取方法和设置方法?即,创建getter和setter的任何属性名称 是不是 已经定义。 这个概念可以在PHP中使用和魔术方法(有关这些信息,请参见PHP文档),因此我真的在问是否存在与这些方法等效的JavaScript? 不用说,理想情况下,我想要一个跨浏览器兼