laravel map the output of the api 1

laravel map the output of the api

Booking::select(['booking_date', 'pickup_datetime'])->get()
  ->map(function ($booking) {
      $booking->pickup_datetime = optional($booking->pickup_time)->format('d M Y, h:i A');
      $booking->booking_date = optional($booking->created_at)->format('d M Y, h:i A');
      return $booking;
  });

Here is what the above code is Doing:
1. We are selecting the booking_date and pickup_datetime columns from the Booking model.
2. We are getting the results of the query.
3. We are mapping the results to a new collection.
4. We are formatting the pickup_datetime and booking_date columns to a new format.
5. We are returning the new collection.

Similar Posts