php - 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')。
解决方案
推荐阅读
- mysql - Mysql不断在docker中重新启动和关闭
- r - 如何删除R中每列的前两个字母?
- .net - EF Core:与服务器建立连接成功,但在登录过程中自发发生错误
- laravel - Laravel,如何在控制器中使用 foreach 在数据库中插入多行?
- java - 房间 DAO 返回的对象上的空对象引用
- sql - 如何以递减顺序对列进行排序,如果我们找到一个并列的记录,我们将根据 SQL 中的另一列对它们进行排序?
- firebase - Flutter,更新firebase数据时setstate不起作用
- node.js - 设置数据库数据后在nodejs中加载模块
- node.js - 如何在每次构建之前运行测试套件?
- c# -