【发布时间】:2021-07-06 02:15:26
【问题描述】:
是否有可能获取整个数据库的表及其关联?
我的例子
class Gambler extends Model
{
use HasFactory;
public function horse()
{
return $this->belongsTo(Horse::class, 'horse_id', 'id');
}
}
我正在使用 axios 获取 Gamblers 的数据,这些数据在控制器中与此代码一起返回
public function getGamblers () {
echo Gambler::all();
}
但是我也想获得所有相关的马匹。我可以像这样轻松搞定一个
public function getGamblers () {
echo Gambler::find(1)->horse;
}
但也许有可能做这样的事情,在我的情况下这是行不通的
public function getGamblers () {
echo Gambler::all()->horse;
}
【问题讨论】: