John Davidson

php - SOVLED Merge and get two collections with the same relationship

0 comments
Message:


------ ADDED -------

It looks like a bulletin board.

Users can create "channels" and

You can enter "Channel" and write "Post"

From here.

The "channel" I created is

It will be entered into the channels model

When I join a "channel" created by someone else, I enter the channels_joins model.


Simply

in the channel model
Information such as channel name, creator, etc.

Channel joins model contains only channel_id and user_id, and each id is connected with a foreigner key.


I combine the "channels" I created and the "channels" I subscribed to in the view and show them as a list

The code is below


        $joins = $this->channelJoins()
->join('channels', 'channel_joins.channel_id', '=', 'channels.id')
->get();
$channels = $this->channels()
->leftJoin('channel_joins', 'channels.id', '=', 'channel_joins.channel_id')
->select('channels.*', 'channel_joins.channel_id', 'channel_joins.user_id')
->get();
$vals = collect($joins)->merge($channels);

I need to iterate over the list in the view and show two data.



  1. Show channel name

  2. Number of people who are subscribed to the channel


Here's why I'm stuck

When checked with dd(), $list consists of one collection, and there are two models "App\Models\Channels" and "App\Models\Channel_joins" in it.



In the case of a channel model, the channel name is not a problem, but the problem is to print the number of people who are subscribed.

In case of channel joins model, it is a problem to output the channel name.


I


            @forelse ($posts as $list)
<tr>
<td>
<div class="p-1 ml-1">
<a href="{{ route('channel.show', $list->id) }}">{{ $list->name }}</a>
<p class="float-right">
Count:&nbsp;
<span class="total">
{{-- {{ dd($list) }}--}}
{{ ($list->count('user_id')) }}
</span>
&nbsp; EA
</p>
</div>
</td>
</tr>
@endforeach

------ ADDED ---------


Here is


Channels Table


Channel Joins Table


Users Table


Thanks for reply

I have one channel called a, but in the picture, it is output as two.


In addition, the number of people who are subscribed to channel a is 2, and the other channels have 0-1 people. In the picture, the wrong value is exposed.


how to modify my query to right way ?




My Answer:
posts and comments on the bulletin board._Posts can have multiple comments._Each post and comment has a user_id to indicate who created it._We have two collections:_1. posts collection with fields: post_id, user_id, content_2. comments collection with fields: comment_id, post_id, user_id, content_We want to merge these two collections and get a new collection with the following fields:_- post_id- user_id (of the post creator)- content (of the post)- comments (an array of comments with fields: comment_id, user_id (of the comment creator), content)_Here's the PHP code to achieve this:_php $post['post_id'], 'user_id' => $post['user_id'], 'content' => $post['content'], 'comments' => [] ]; foreach ($comments as $comment) { if ($comment['post_id'] === $post['post_id']) { $newPost['comments'][] = [ 'comment_id' => $comment['comment_id'], 'user_id' => $comment['user_id'], 'content' => $comment['content'] ]; } } $newCollection[] = $newPost;}print_r($newCollection);?>This code will merge the posts and comments collections based on the post_id and create a new collection with the desired structure.

Rate this post

3 of 5 based on 9878 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us