DEV Community

Discussion on: How to Optimize Database Queries in Laravel?

Collapse
 
xorock profile image
xorock

I don't want to filter results. I want to add one column (like id from external query) to all results. I can do:
Post::get()->map(function($item) use ($now) {
return $item->toArray() + ['date' => $now];
});
But, is there a better solution?

Thread Thread
 
readymadecode profile image
Chetan Rohilla

Try this

$posts = Post::all()->map(function ($post) use($custom_value) {
// modify eloquent object here
$post->custom_column = $custom_value;
//apply any condition if available
return $post;
});