Use merge helper to create collection with custom data manually in Laravel

avatar
Published: Jul 10, 2022

Sometimes you need to add some extra collection to another paginate collect. So you can follow this way with merge helper 👇

$books = Book::where('user_id','1')->paginate(3);

$customData = collect(['my_custom_data' => 'Put your custom data here']);

$data = $customData->merge($books);

return response()->json($data);