I can't read all the data table, it only returns the correct first row
P粉645569197
P粉645569197 2023-09-12 15:48:04
0
1
597

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

The problem is that the returned collection only got the name and location of the first date.

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;

}

The results I get in Postman are as follows:

[
    {
        "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 user's appointment list and the information for each appointment (date, time, date, post_name, post_place)

P粉645569197
P粉645569197

reply all(1)
P粉718730956

The problem is trying to modify the original appointment to get the result, please check your code

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

The results must be placed in different variables to get the correct results

// 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;
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template