Php Laravel 3表模型关系构造

Php Laravel 3表模型关系构造,php,laravel,laravel-4,Php,Laravel,Laravel 4,您好,我有以下3个表,它们相互关联,ID如下,我试图在Laravel中为每个表创建一个模型,但我不知道如何使用belongsTo和others TABLE: posts ----------------------------- id Auto-inc type_id if (3 its for a blog) title content TABLE: blog ----------------------------- id Auto-in

您好,我有以下3个表,它们相互关联,ID如下,我试图在Laravel中为每个表创建一个模型,但我不知道如何使用belongsTo和others

 TABLE: posts
 -----------------------------
 id        Auto-inc
 type_id   if (3 its for a blog)
 title 
 content

 TABLE: blog
 -----------------------------
 id        Auto-inc
 post_id   the post id
 cat_id    blog_cat table id 
 slug


 TABLE:blog_cat
 -----------------------------
 id    Auto-inc
 cat_name 
现在我正在尝试为Blog.php、BlogCat.php和Post.php编写模型,在这里我可以使用如下内容:

$blog = Post::with('blogCat')->get()->toArray();
要使用post&if type_id=3 blog获取一个数组,还要从blog表中获取一个blog信息数组,这是可能的还是我只需要使用select、where/Join


谢谢

在您的博客模型中,您需要一个返回BelongsTo的猫关系

在Post模型中,您需要一个返回HasOne的blog关系

要获取已加载博客的帖子集合,请执行以下操作:

$blog = Post::with('blog')->get()
要获取每个博客都已加载Blogs eager和BlogCats eager的帖子集合,请执行以下操作:

$blog = Post::with('blog.blogCat')->get()

为了只为类型id为3的帖子加载Blog,你需要使用一个

来说明我在这里到底混淆了什么?你说的是Blog模型,你的例子说的是Post::with'Blog',你的意思是$Blog=Blog::with'Post'->get->toArray@user3150060我编辑了这篇文章来澄清。不,你不需要使用ToArray。ToArray将模型转换为属性数组。get返回一个类似于数组的集合。我建议你仔细阅读我链接到的页面,以了解关系。你能为我构建博客和cat模型吗?