在 Laravel 4 中发送模型的嵌套数组的最佳方法是什么?

What is the best way to send a nested array of a model in Laravel 4?

我正在创建一个包含 post 列表的简单博客。每个 post 都有多种媒体(图片、视频等)。所以我有三个模型:Post (id, title, article, album_id, etc.), Album (id, title, description, etc.) and Media (id, url,类型等)。一个 post 可以有一个相册,一个相册可以有多个媒体。

为了生成包含 post 列表的页面,我编码如下 (BlogController):

public function generateList(){
        $posts = Post::where('subcategory_id','=','3')->orderBy('created_at', 'DESC')->paginate(30);

        return View::make('main.informativos')
            ->with('posts', $posts);
    }

如您所见,今天只有 post 被发送到视图。我的问题是:发送每个 post 的所有媒体的最佳方式是什么?记得有很多 posts.

在模型中随机指定外键
使用 'foreignkey'、'primarykey'
的关系函数 在 where condition
之前在控制器中使用 with function 公司、国家职能基于关系
你再次怀疑..检查laravel doc

public function index()
{
    $Location = Location::with('Company', 'Country')
        ->where('company_id', '=', $company_id)
        ->get();
    return response()->json($Location, 200);
}