简体   繁体   English

来自两个表的数据在连接时重复

[英]Data from Two tables duplicating on join

I have got two tables , I would use facebook POST as an example. 我有两个表,我将以facebook POST为例。

Post Table 发布表

Comment Table 评论表

My query 我的查询

   $result = DB::table('posts')
            ->join('comments', 'posts.post_ID', '=', 'comments.post_ID')
            ->get();

I will receive an array of posts and comments merge. 我将收到一系列帖子和评论合并。 For each comments that exist , they will have the posts data. 对于每个存在的评论,他们将拥有帖子数据。

What i want is to be able to do something like 我想要的是能够做类似的事情

foreach($posts as $post){
     foreach($post['comment']{

     }
}

Any idea how i can do that? 知道我该怎么做吗?

Something like this should work: 这样的事情应该起作用:

$result = DB::table('posts')
        ->join('comments', 'posts.id', '=', 'comments.post_id')
        ->get();

In the view: 在视图中:

@foreach($posts as $post)

    {{ $post->post_title }}
    {{ $post->message }}

@endforeach

Make sure that, field names in ->join('comments', 'posts.id', '=', 'comments.post_id') are right or change accordingly. 确保->join('comments', 'posts.id', '=', 'comments.post_id')中的字段名称正确或相应更改。 post_title/comment_text is used to demonstrate the example, change to it's original table's field name and {{ }} is used in the example to echo the data, if you are not using Blade then use echo $post->post_title instead. post_title/comment_text用于演示示例,将其更改为原始表的字段名称,示例中使用{{ }}来回显数据,如果您不使用Blade则使用echo $post->post_title代替。

Update:: 更新::

If you use Eloquent then use: 如果您使用Eloquent使用:

// Assumed you have defined comments as relationship method
$posts = Post::with('comments')->get(); // it's faster and called eager loading

Then in the view: 然后在视图中:

@foreach($posts as $post)

    {{ $post->post_title }}

    @foreach($post->comments as $comment)

        {{ $comment->message }}

    @endforeach

@endforeach

I will receive an array of posts and comments merge. 我将收到一系列帖子和评论合并。 For each comments that exist , they will have the posts data. 对于每个存在的评论,他们将拥有帖子数据。

This is correct SQL behavoir when using a join. 使用联接时,这是正确的SQL行为。 You will get the contents of both the rows inside the posts and comments rows on your JOIN . 您将在JOIN上获得posts内的行和comments行的内容。

According to Laravel 4.2's documentation on the join method , the parameters of the join function are: 根据Laravel 4.2关于join方法的文档 ,join函数的参数为​​:

join($table, $one, $operator = null, $two = null, $type = 'inner', $where = false)

Using an INNER JOIN , you are only going to get rows returned to you with your query (using an inner join) if you have a comment for all of the posts that you want data from. 使用INNER JOIN ,如果对要从中获取数据的所有帖子都有评论,则只会使用查询(使用内部INNER JOIN获得返回的行。 Additionally, with your INNER JOIN , if there is no comment on your post, you will not get any posts returned to you. 此外,使用INNER JOIN ,如果您的帖子没有评论,您将不会收到任何帖子。

Also, you are not going to be able to separate all of your comments from your posts, which may mean that you are getting results returned for posts that you 此外,您将无法将所有评论与帖子分开,这可能意味着您正在为自己发布的帖子返回结果

The simple way to solve this for you would be to make two eloquent models: 为您解决此问题的简单方法是建立两个雄辩的模型:

class Post extends Eloquent {
    protected $table = 'posts';

    public function getComments() {
        return $this->hasMany('Comments', 'post_id');
    }
}

class Comments extends Eloquent {
    protected $table = 'comments';
}

From that you can query for all of the posts with eager loading : 从那里,您可以查询所有渴望加载的帖子:

$posts = Post::with('comments')->get();

and inside your view, you go: 在视图中,您可以:

foreach($posts as $post) {
    // echo the $post? (title, author etc)
    foreach($post->comments() as $comment) {
       // echo the $comment? (author, content etc)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM