skip to Main Content

I want to return a list of appointments for a given user (Appointment table contains user_id and post_id).

The problem is, the returned collection only got the name and place for the first appointment.

public function index()
{
    // Retrieve all appointments from the user
    $appointments = Appointment::where('user_id', Auth::user()->id)->get();

    // Retrieve all post IDs associated with the appointments
    $postIds = $appointments->pluck('post_id')->toArray();

    // Retrieve all posts with the associated post IDs
    $posts = Post::whereIn('id', $postIds)->get()->keyBy('id');

    // Assign post details to each appointment
    $appointments = $appointments->map(function ($appointment) use ($posts) {
        $postId = $appointment->post_id;

        if (isset($posts[$postId])) {
            $post = $posts[$postId];
            $appointment->name = $post->name;
            $appointment->place = $post->place;
        } else {
            // For debugging purposes, log the missing post details
            Log::info("Post details not found for appointment: " . $appointment->id);
        }
        return $appointment;

    });

    return $appointments;

}

And the result I got in postman is below:

[
    {
        "id": 4,
        "user_id": 9,
        "post_id": 2,
        "date": "6/14/2023",
        "day": "Wednesday",
        "time": "12:00 PM",
        "status": "upcoming",
        "created_at": "2023-06-12T17:19:58.000000Z",
        "updated_at": "2023-06-12T17:19:58.000000Z",
        "name": "Biskra",
        "place": "Tolga"
    },
    {
        "id": 5,
        "user_id": 9,
        "post_id": 9,
        "date": "6/24/2023",
        "day": "Saturday",
        "time": "14:00 PM",
        "status": "cancel",
        "created_at": "2023-06-12T18:53:45.000000Z",
        "updated_at": "2023-06-12T18:53:45.000000Z"
    },
    {
        "id": 6,
        "user_id": 9,
        "post_id": 8,
        "date": "6/17/2023",
        "day": "Saturday",
        "time": "12:00 PM",
        "status": "complete",
        "created_at": "2023-06-13T01:43:14.000000Z",
        "updated_at": "2023-06-13T01:43:14.000000Z"
    }
]
 

I want to get the list of appointments of user with the information of each appointment (date, time, day, post_name, post_place)

2

Answers


  1. The issue is with the attempt to modify original appointments to get the results, Please check your code

        // Assign post details to each appointment
        
        $appointments = $appointments->map(function ($appointment) use ($posts) {
        }
    

    The results has to be placed in different variable to get the correct result out

    // Assign post details to each appointment
    $allTheRecords = $appointments->map(function ($appointment) use ($posts) {
        $postId = $appointment->post_id;
    
        if (isset($posts[$postId])) {
            $post = $posts[$postId];
            $appointment->name = $post->name;
            $appointment->place = $post->place;
        } else {
            // For debugging purposes, log the missing post details
            Log::info("Post details not found for appointment: " . $appointment->id);
        }
    
        return $appointment;
    });
    
    return $allTheRecords;
    
    Login or Signup to reply.
  2. You can use Eloquent: API Resources if you are building an API to transform the data as you need.

    Change your index function to eager load post with appointments and return a
    resource collection.

    public function index()
    {
        // Retrieve all appointments for the user with posts
        $appointments = Appointment::with('post')->where('user_id', Auth::user()->id)->get();
        return AppointmentResource::collection($appointments);
    }
    

    If your Appoinment model does not already have a post relation, add it.

    public function post()
    {
        return $this->belongsTo(Post::class);
    }
    

    Create a new resource using below command

    php artisan make:resource AppointmentResource
    

    In your AppHttpResourcesAppointmentResource;

    public function toArray(Request $request): array
    {
        return [
            "id": $this->id,
            "user_id": $this->user_id,
            "post_id": $this->post_id,
            "date": $this->date,
            "day": $this->day,
            "time": $this->time,
            "status": $this->status,
            "created_at": $this->created_at,
            "updated_at": $this->updated_at,
            "name": $this->post->name,
            "place": $this->post->place
        ];
    }
    

    If you don’t need to transform data and just need to access the data you can just access post relation when you are iterating in the view like $appoinment->post->name.

    @foreach ($appointments as $appointment)
        {{ $appointment->post->name }}
    @endforeach
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search