我试图从一个与我目前使用的模型没有直接关系的表中检索数据。
我的数据结构:
表:posts
表:post_stacks
表:堆栈
我的雄辩模型来自Post.php ( post表),我正在尝试获取与post关联的所有堆栈(从堆栈表)。我只想在Post.php上声明我的关系,而不是我的枢轴表(post_stacks)。
我尝试过hasManyThrough,但是我的表的结构并不像Laravel所要求的那样“符合”需求,因为我需要一个外键在我的堆栈表上。
以下是我目前的实现:
Post.php
class Post extends Model
{
protected $dates = [
'created_at',
'updated_at'
];
public function user()
{
return $this->belongsTo(\App\User::class, 'user_id', 'id');
}
public function post_stacks()
{
return $this->hasMany(\App\PostStack::class);
}
public function post_os()
{
return $this->hasMany(\App\PostOS::class, 'post_id', 'id');
}
public function post_tags()
{
return $this->hasMany(\App\PostTag::class , 'post_id', 'id');
}
public function getCreatedAtAttribute($value)
{
return Carbon::parse($value)->toFormattedDateString();
}
}
PostController.php
class PostController extends Controller
{
public function index()
{
$posts = Post::all();
foreach($posts as $post){
$post->user;
$post->created_at;
$posts_os = $post->post_os;
$post_stacks = $post->post_stacks;
$post_tags = $post->post_tags;
foreach($posts_os as $post_os){
$os = OS::where('id', $post_os->os_id)->first();
$post_os['body'] = $os['body'];
}
foreach($post_stacks as $post_stack){
$stack = Stack::where('id', $post_stack->stack_id)->first();
$post_stack['url'] = $stack['url'];
$post_stack['body'] = $stack['body'];
}
foreach($post_tags as $post_tag){
$tag = Tag::where('id', $post_tag->tag_id)->first();
$post_tag['body'] = $tag['body'];
}
}
return response()->json($posts);
}
}
我的JSON数据响应
[
{
"id":1,
"title":"Laravel + XAMPP",
"user_id":1,
"description":"I'll take you through the entire process of setting up a development environment for Laravel using XAMPP.",
"created_at":"Jun 12, 2017",
"updated_at":"2017-06-12 08:55:02",
"user":{
"id":1,
"name":"EpIEhg7ciO",
"email":"AiyZXrubVG@gmail.com",
"created_at":"2017-06-12 08:55:02",
"updated_at":"2017-06-12 08:55:02"
},
"post_os":[
{
"id":1,
"post_id":1,
"os_id":1,
"created_at":"2017-06-12 08:55:02",
"updated_at":"2017-06-12 08:55:02",
"body":"Windows"
}
],
"post_stacks":[
{
"id":1,
"post_id":1,
"stack_id":1,
"created_at":"2017-06-12 08:55:02",
"updated_at":"2017-06-12 08:55:02",
"url":"laravel.svg",
"body":"Laravel"
},
{
"id":2,
"post_id":1,
"stack_id":2,
"created_at":"2017-06-12 08:55:02",
"updated_at":"2017-06-12 08:55:02",
"url":"xampp.svg",
"body":"XAMPP"
}
],
"post_tags":[
{
"id":1,
"post_id":1,
"tag_id":1,
"created_at":"2017-06-12 08:55:02",
"updated_at":"2017-06-12 08:55:02",
"body":"laravel"
},
{
"id":2,
"post_id":1,
"tag_id":2,
"created_at":"2017-06-12 08:55:02",
"updated_at":"2017-06-12 08:55:02",
"body":"xampp"
}
]
},
{
"id":2,
"title":"Laravel + Vagrant",
"user_id":1,
"description":"I'll take you through the entire process of setting up a development environment for Laravel using Vagrant.",
"created_at":"Jun 12, 2017",
"updated_at":"2017-06-12 08:55:02",
"user":{
"id":1,
"name":"EpIEhg7ciO",
"email":"AiyZXrubVG@gmail.com",
"created_at":"2017-06-12 08:55:02",
"updated_at":"2017-06-12 08:55:02"
},
"post_os":[
{
"id":2,
"post_id":2,
"os_id":1,
"created_at":"2017-06-12 08:55:02",
"updated_at":"2017-06-12 08:55:02",
"body":"Windows"
},
{
"id":3,
"post_id":2,
"os_id":2,
"created_at":"2017-06-12 08:55:02",
"updated_at":"2017-06-12 08:55:02",
"body":"Mac OS X"
},
{
"id":4,
"post_id":2,
"os_id":3,
"created_at":"2017-06-12 08:55:02",
"updated_at":"2017-06-12 08:55:02",
"body":"Linux"
}
],
"post_stacks":[
{
"id":3,
"post_id":2,
"stack_id":1,
"created_at":"2017-06-12 08:55:02",
"updated_at":"2017-06-12 08:55:02",
"url":"laravel.svg",
"body":"Laravel"
},
{
"id":4,
"post_id":2,
"stack_id":3,
"created_at":"2017-06-12 08:55:02",
"updated_at":"2017-06-12 08:55:02",
"url":"vagrant.png",
"body":"Vagrant"
}
],
"post_tags":[
{
"id":3,
"post_id":2,
"tag_id":1,
"created_at":"2017-06-12 08:55:02",
"updated_at":"2017-06-12 08:55:02",
"body":"laravel"
},
{
"id":4,
"post_id":2,
"tag_id":3,
"created_at":"2017-06-12 08:55:02",
"updated_at":"2017-06-12 08:55:02",
"body":"vagrant"
}
]
}
]
我的JSON数据正是我想要的。我只是认为我的PostController实现效率低下,运行了太多的查询。我运行了太多的查询和嵌套循环。有没有一个干净的方法,我可以建立一个关系使用拉拉维尔的方法/关系?
谢谢!
发布于 2017-06-17 20:30:58
你能在Stack模型上声明关系吗?您只排除在透视表上添加关系。如果是的话:
class Post extends Model
{
public function stacks()
{
return $this->hasMany(\App\Stack::class);
}
}
class Stack extends Model
{
public function posts()
{
return $this->belongsToMany(\App\Post::class);
}
}根据PostStack模型中定义的字段,这应该是使关系正常工作所需的全部内容。此外,您还可以访问pivot属性,允许您:
$post->pivot->somePropertyOnStack编辑
摘自这些文档,让你对雄辩如何决定关系有一个大致的认识:
记住,雄辩将自动确定正确的外键列的评论模式。按照惯例,雄辩者将取所属模型的“蛇案”名称,并以_id作为后缀。因此,对于这个示例,雄辩者将假设注释模型上的外键是post_id。
至于与联合表格的关系:
要确定关系的连接表的表名,雄辩者将按字母顺序加入两个相关的模型名称。
文档的这一部分将解释每种关系类型以及它们如何雄辩地发生:
https://laravel.com/docs/5.4/eloquent-relationships#defining-relationships
https://stackoverflow.com/questions/44609068
复制相似问题