Laravel雄辩的关系基于嵌套父母的价值观

时间:2020-03-26 22:05:27

标签: php laravel eloquent relationship has-one

结构为USER(有多个)TIMERS(有一个)TIMER_THEME(有多个)TIMER_THEME_OPTIONS(有一个基于USER_ID的)USER_TIMER_OPTIONS

除了用户特定的选项外,所有方法均有效,因为我无法从TIMER中获取USER_ID。

在定义关系时是否可以访问数据?

示例查询:

Timer::find(1)->with('timer_theme.timer_theme_options.user_timer_option')->get();

TIMER_THEME_OPTIONS模型。 (我想做这样的事情)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class TimerThemeOptions extends Model
{

    protected $fillable = [
        'id',
        'timer_theme_id',
        'type',
        'name',
        'selector',
        'attributes',
        'default_value'
    ];

    public function user_timer_option()
    {
        //return $this->timer_theme();
        return $this->hasOne(UserTimerOptions::class)->where('user_id', '=', $this->timer_theme()->timer()->id);
    }

    public function timer_theme() {
        return $this->belongsTo(TimerTheme::class);
    }
}

0 个答案:

没有答案