Have a look at global scopes
Jun 6, 2019
15
Level 1
Create an up to date feed
Hi everyone, i've a 3 entities called Opportunity, Reply and Comments. I would like to order every item by the created_at attribute (last created first).
For this i've created 3 different query
$newsReply = Reply::orderBy('id', 'desc')->take(3)->get();
$newsOpportunity = Opportunity::orderBy('id', 'desc')->take(3)->get();
$newsComment = Comment::orderBy('id', 'desc')->take(3)->get();
And then i put it into an array, so the result is like this
array:3 [▼
0 => Collection {#676 ▼
#items: array:3 [▼
0 => Reply {#677 ▼
#fillable: array:15 [▼
0 => "opportunity_id"
1 => "association_id"
2 => "media_name"
3 => "title"
4 => "description"
5 => "changing_number"
6 => "like"
7 => "winner"
8 => "coherence"
9 => "creativity"
10 => "clarity"
11 => "personal"
12 => "average"
13 => "cover"
14 => "declined"
]
#connection: "mysql"
#table: "replies"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:17 [▼
"id" => 4
"opportunity_id" => 2
"association_id" => 1
"title" => "dwfnkjwenf"
"description" => "aa"
"changing_number" => 0
"winner" => 0
"coherence" => 0.0
"creativity" => 0.0
"clarity" => 0.0
"personal" => 0.0
"average" => 0.0
"cover" => "1559037254.jpg"
"document_file" => null
"created_at" => "2019-05-28 11:54:14"
"updated_at" => "2019-05-29 09:33:15"
"declined" => 0
]
#original: array:17 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [▶]
}
1 => Reply {#678 ▶}
2 => Reply {#679 ▶}
]
}
1 => Collection {#713 ▼
#items: array:3 [▼
0 => Opportunity {#714 ▶}
1 => Opportunity {#715 ▶}
2 => Opportunity {#716 ▶}
]
}
2 => Collection {#750 ▼
#items: array:3 [▼
0 => Comment {#751 ▶}
1 => Comment {#752 ▶}
2 => Comment {#753 ▶}
]
}
]
Or if you have an easier way to do so it's welcome
Level 41
Hey I found the solution here: https://medium.com/@tadaspaplauskas/quick-tip-laravel-eloquent-collections-merge-gotcha-moment-e2a56fc95889
Its because reply and news have the same id. So this should work
$merged = new \Illuminate\Database\Eloquent\Collection;
foreach ($newsReply as $reply)
$merged->push($reply);
foreach ($newsOpportunity as $opportunity)
$merged->push($opportunity);
foreach ($newsComment as $comment)
$merged->push($comment);
1 like
Please or to participate in this conversation.