首页 > 解决方案 > Laravel 雄辩的条件关系

问题描述

我正在尝试根据表列的值在 eloquent 模型中建立条件关系,但它不起作用。

这是我正在使用的代码:

 //RELATIONS
public function task_schedule()
{
    if ($this->task_schedule_id === 0) {
        return $this->belongsTo('TaskSchedule', 'hold_task_schedule_id', 'id');
    } else {
        return $this->belongsTo('TaskSchedule');
    }

}

基本上我想使用不同的列来让我的子模型定义在属于关系中。

scopeProfile($query) 对我不起作用,因为我不想在每个查询中都检索孩子,而且在很多代码部分都使用 Task->with('task_schedule')。

标签: phplaraveleloquent

解决方案


推荐阅读