我正在学习使用Laravel的MVC模式,而Eloquent 的模型对我来说是一种压倒性的感觉.Eloquent 模型返回表的列的集合,但我希望另一个表的集合成为该模型的属性.
我有三张桌子
Books个
id,
title
Contribution个
book_id,
contributor_id,
role
Author个
id,
name
我想要更改函数的返回值,如Model Books的all()、find()、get()等,以包括贡献者
从这一点开始:
[ "id" => "1",
"title" => "CS101"
]
对此:
[ "id" => "1",
"title" => "CS101",
"contributors" => [
[
"id" => "1",
"name" => "Einstein"
"role" => "author"
],
[
"id" => "2",
"name" => "Thomas"
"role" => "translator"
]
]
]
我是不是应该使用Eloquent 并构建自己的模型,或者是否有可以覆盖的Eloquent 函数或更好的方法来实现这一点