【发布时间】:2018-03-15 10:58:19
【问题描述】:
我将 Slim 与 Eloquent 结合使用,并且遇到了一些问题,即嵌套关系在选择特定列时返回 null。
我的代码如下所示:
class User extends Model {}
class Task extends Model {
public function publisher() {
return $this->belongsTo('App\User', 'published_by');
}
}
class Package extends Model {
public function task() {
return $this->belongsTo('App\Task', 'task_id');
}
}
奇怪的事情发生了:
// Works well, I got publisher info and task info, but some columns are useless
// I just want to hide them and (may) improve performance in this case
Package::with('task.publisher:id,nick')->get();
// I got a null publisher
Package::with('task:id,title', 'task.publisher:id,nick')->get();
// Error: Unknown column `publisher`
Package::with('task:id,title,publisher')->get();
Package::with('task:id,title,publisher:id')->get();
如何获得带有 Task 和 Task's 发布者的 Package 模型(这是 User 模型,而仅返回 Task 和 User 的特定列?
谢谢。
【问题讨论】:
-
您可以回答自己的问题。将帖子中受人尊敬的部分放在答案部分。而不是接受它作为答案。
-
我将您的解决方案移至社区 wiki 答案。
-
非常感谢,由于某些原因我这几天没有查看通知,现在我知道了诀窍。
标签: php laravel eloquent lumen