Laravel pluck fields from relations
You can use Laravel’s pluck method as: $sellers = Seller::with(‘user’)->get()->pluck(‘user.first_name’, ‘id’)
You can use Laravel’s pluck method as: $sellers = Seller::with(‘user’)->get()->pluck(‘user.first_name’, ‘id’)
A foreach statement should be used as a sort of a way to cycle through a collection and perform some sort of logic on it. If what is in it effects other things in the program, then use this loop. The .each method uses array_map to cycle through each of the objects in the collection … Read more
The merge method returns the merged collection, it doesn’t mutate the original collection, thus you need to do the following $original = new Collection([‘foo’]); $latest = new Collection([‘bar’]); $merged = $original->merge($latest); // Contains foo and bar. Applying the example to your code $related = new Collection(); foreach ($question->tags as $tag) { $related = $related->merge($tag->questions); }
When using ->get() you cannot simply use any of the below: if (empty($result)) { } if (!$result) { } if ($result) { } Because if you dd($result); you’ll notice an instance of Illuminate\Support\Collection is always returned, even when there are no results. Essentially what you’re checking is $a = new stdClass; if ($a) { … … Read more