是否可以一次获取多行的关系数据
就像我有countries
表和cities
表一样,在cities
中它与国家/地区有关系(有或没有)
所以如果我想从Country Eloquent获取所有与国家有关系的城市(可能)
我已经尝试过类似的事情
Model::where(...)->get()->methods();
或
Model::all()->relations;
我们可以这样做吗?
答案 0 :(得分:1)
使用with()
来加载和has()
因此,如果没有cities
,请确保国家/地区有cities
,然后Country
将不会返回
Country::with('cities')->has('cities')->get();
答案 1 :(得分:0)
您可以为此使用嵌套的紧急加载。
Model::with('cities')->all();
来源:https://laravel.com/docs/7.x/eloquent-relationships#eager-loading