cari

Rumah  >  Soal Jawab  >  teks badan

Saya tidak boleh membaca semua jadual data, ia hanya mengembalikan baris pertama yang betul

Saya ingin mengembalikan senarai janji temu untuk pengguna tertentu (jadual janji temu mengandungi user_idpost_id).

Masalahnya, koleksi yang dikembalikan hanya mendapat nama dan lokasi tarikh pertama.

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;

}

Keputusan yang saya dapat dalam Posmen adalah seperti berikut:

[
    {
        "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"
    }
]

Saya ingin mendapatkan senarai temujanji pengguna dan maklumat untuk setiap temujanji (tarikh, masa, tarikh, nama_pos, tempat_jawatan)

P粉645569197P粉645569197511 hari yang lalu637

membalas semua(1)saya akan balas

  • P粉718730956

    P粉7187309562023-09-13 13:02:16

    Masalahnya cuba mengubah suai janji temu asal untuk mendapatkan keputusan, sila semak kod anda

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

    Hasil mesti diletakkan dalam pembolehubah yang berbeza untuk mendapatkan hasil yang betul

    // 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;

    balas
    0
  • Batalbalas