Laravel如何加入一对多关系表

问题描述 投票:0回答:4

我正在使用Laravel 5.5,我希望通过连接具有一对多关系的表来显示数据列表。

目前,我通过遍历循环并进行查询来检索数据。我认为这种方式效率非常低,因为如果我要显示1000行数据记录,我将不得不使用1000个循环来追加具有一对多关系的其他数据。

我正在考虑使用缓存解决这个问题,但似乎并没有解决根本问题。

为了更多的理解,我已经共享了我想要加入的表,如下所示。

邮政表

| id | comment_id | status |
|----|------------|--------|
| 1  | 1          | 0      |
| 2  | 2          | 0      |
| 3  | 3          | 1      |
| 4  | 4          | 0      |
| 5  | 5          | 1      |
| 6  | 6          | 1      |

如何表

| id | order_id | content  |
|----|----------|----------|
| 1  | 1        | hi       |
| 2  | 1        | hellow   |
| 3  | 1        | yes      |
| 4  | 1        | okay     |
| 5  | 2        | bye      |
| 6  | 2        | good bye |

如果我要使用表Post加入表Comment,因为它们有一对多的关系,行不匹配。我如何加入这两个表来显示带评论的帖子列表?

样品列表控制器

/**
 * @param Request $request
 * @return \Illuminate\Http\JsonResponse
 */
public function list(Request $request)
{
    $vaildData = $request->validate([
        'comment_id' => 'numeric',
    ]);

    $posts = new PostModel;
    $posts->find(1);
    $displayPosts = [];

    foreach ( $posts->find(1)->get() as $post ) {
        $displayPosts->comments = $post->comment()->get();
    }

    return $displayPosts;
}

Post Model命名空间App \ Model \ Post;

use SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public function comments()
    {
        return $this->hasMany('App\Model\Post\Comment’, ‘post_id', 'id');
    }
}
mysql laravel laravel-5
4个回答
4
投票

使用with()急切加载您的评论。

$posts = PostModel::with('comments')->find($id);

所以你的功能将像 -

public function list(Request $request)
{
   $vaildData = $request->validate([
    'comment_id' => 'numeric',
   ]);

   $posts = PostModel::with('comments')->find(1);
   return $displayPosts;
}

您可以使用whereHas()使用comment_id过滤您的评论,如下所示 -

$comment_id = $request->input('comment_id');
$posts = PostModel::with('comments')->whereHas('comments', function ($query) use($comment_id)
      {
        $query->where('id', '=', $comment_id);
      })->find(1);

3
投票

https://laravel.com/docs/5.1/eloquent-relationships

首先,您可以参考此文档。

为Post和Comment表设置一对多关系:

  1. 一篇文章有​​很多评论
  2. 所以在你的评论表中应该有一个名为post_id的列
  3. 在你的Post.php里面

public function comments()
{
    return $this->hasMany('App\Comment’);
}
  1. 在你的控制器内

public function list(Request $request){
    $posts = Post::where('id', 1)
                  ->with('comments')
                  ->get()

    return $posts;
}

2
投票

public function list(Request $request, $id)

{

$vaildData = $request->validate([ 'comment_id' => 'numeric',]);

$posts = PostModel::find($id);

return $posts->comments;

}

试试这个...希望这可以帮到你

或者你可以尝试这样

public function list(Request $request, $id)

{

$vaildData = $request->validate([ 'comment_id' => 'numeric',]);

$posts = PostModel::find($id)->comments()->get();

return $posts;

}


2
投票
public function list(Request $request)
{
    $vaildData = $request->validate([
        'comment_id' => 'numeric',
    ]);

    $posts = new PostModel;
    $results = $posts->find(1)->with('comments')//comments is the function name you defined in the App\Model\Post
    return resurts;
}

集合结果包含帖子的信息以及属于帖子的另一个额外评论集合

© www.soinside.com 2019 - 2024. All rights reserved.