App\Model::with('relation') 在 Laravel 7.x 控制器中等效
Equivalent for App\Model::with('relation') in Laravel 7.x controller
在我的控制器中,我 return 一个 json 对象:
public function show(Comic $comic)
{
//
return Comic::with('series')->findOrFail($comic->comic_id);
}
是否有更短的形式来获得相同的输出?我在想
public function show(Comic $comic)
{
//
return $comic->with('series');
}
但这不起作用并抛出错误:
TypeError: Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given
谢谢。
当然,有“Lazy Eager Loading”这样的东西:
$comic->load('series');
这会在您已有的模型实例上为您加载该关系,这样您就不必再次查询该模型。 load
return 模型实例,因此您可以 return 调用的结果作为模型实例。
Laravel 7.x Docs - Eloquent - Relationships - Eager Loading - Lazy Eager Loading load
在我的控制器中,我 return 一个 json 对象:
public function show(Comic $comic)
{
//
return Comic::with('series')->findOrFail($comic->comic_id);
}
是否有更短的形式来获得相同的输出?我在想
public function show(Comic $comic)
{
//
return $comic->with('series');
}
但这不起作用并抛出错误:
TypeError: Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given
谢谢。
当然,有“Lazy Eager Loading”这样的东西:
$comic->load('series');
这会在您已有的模型实例上为您加载该关系,这样您就不必再次查询该模型。 load
return 模型实例,因此您可以 return 调用的结果作为模型实例。
Laravel 7.x Docs - Eloquent - Relationships - Eager Loading - Lazy Eager Loading load