skip to Main Content

I have a student list table. In the table, there is a column to display study programs. This column retrieves data from the study program database. However, what is displayed in the table is the study program id. What I want is that the study program column can display the name of the study program based on the id. How can I change the syntax in my code?

This is model

public function prodi()
{
    return $this->belongsTo(Prodi::class, 'prodi');
}

public function toSearchableArray(): array
{
    return [
        'nama' => $this->nama,
        'nim' => $this->nim,
    ];
}

}

This is controller

public function index(Request $request)
{
    if ($request->keyword) {
        $mahasiswa = Mahasiswa::where('is_delete', null)->search($request->keyword)->paginate(PHP_INT_MAX);
    } else {
        $mahasiswa = Mahasiswa::where('is_delete', null)->paginate(PHP_INT_MAX);
    }

    //return view('mahasiswa.daftar_mahasiswa')->with('mahasiswa', $mahasiswa);

    return view('mahasiswa.daftar_mahasiswa', compact('mahasiswa'));
}

public function create()
{
    $prodis = Prodi::get();
    return view('mahasiswa.tambah_daftar_mahasiswa', compact('prodis'));
}


public function store(Request $request)
{
    $mahasiswa = new Mahasiswa;
    $mahasiswa->nim = $request->nim;
    $mahasiswa->nama = $request->nama;
    $mahasiswa->prodi = $request->prodi;
    $mahasiswa->j_kel = $request->j_kel;
    $mahasiswa->save();

    return redirect()->route('mahasiswa.index');
}

public function show($id)
{
    return view('mahasiswa.daftar_mahasiswa-detail', [
        'mahasiswa' => Mahasiswa::findOrFail($id),
    ]);
}

public function edit($id)
{
    $mahasiswa = Mahasiswa::findOrFail($id);
    $prodis = Prodi::all(); // Mengambil semua data program studi

    return view('mahasiswa.edit_daftar_mahasiswa', compact('mahasiswa', 'prodis'));
}

public function update(Request $request, $id)
{
    $mahasiswa = Mahasiswa::find($id);

    $mahasiswa->update([
        'nim' => $request->get('nim'),
        'nama' => $request->get('nama'),
        'j_kel' => $request->get('j_kel'),
    ]);

    Alert::success('Berhasil 🎉🥳', 'Berhasil mengubah data mahasiswa');
    return redirect()->route('mahasiswa.index');
}

public function destroy($id)
{
    $mahasiswa = Mahasiswa::findOrfail($id);
    // $mahasiswa->delete();
    $mahasiswa->update([
        'is_delete' => 'deleted',
    ]);

    Alert::success('Berhasil 🎉🥳', 'Berhasil menghapus data mahasiswa');

    return redirect()->route('mahasiswa.index');
}

public function exportpdf()
{
    $datas = Mahasiswa::all();
    $pdf = Pdf::loadView('mahasiswa.pdf', ['datas' => $datas]);

    return $pdf->download('Data Mahasiswa -' . Carbon::now()->format('Y-m-d') . '.pdf');
}

}

This is view

<tbody>
                @foreach ($mahasiswa as $mhs)
                    <tr>
                        <td>{{ $loop->iteration }}</td>
                        <td>{{ $mhs->nama }}</td>
                        <td>{{ $mhs->nim }}</td>
                        <td>{{ $mhs->prodi }}</td>
                        <td>{{ $mhs->j_kel }}</td>
                        <td class="d-flex justify-content-around">
                            <button class="action" style="background: #3C91E6">
                                <a href="{{ route('mahasiswa.show', $mhs->nim) }}" style="color: inherit">
                                    <i class='bx bxs-detail'></i>
                                    Detail
                                </a>
                            </button>
                            <button class="action" style="background: #ff8200">
                                <a href="{{ route('mahasiswa.edit', $mhs->nim) }}" style="color: inherit">
                                    <i class='bx bxs-edit'></i>
                                    Edit
                                </a>
                            </button>
                            <form action="{{ route('mahasiswa.delete', $mhs->nim) }}" method="post">
                                @method('delete')
                                @csrf
                                <input name="_method" type="hidden" value="DELETE">
                                <button type="submit" class="action show_confirm" data-toggle="tooltip"
                                    style="background: #cc2936">
                                    <i class='bx bx-trash'></i>
                                    Hapus
                                </button>
                            </form>
                        </td>

                    </tr>
                @endforeach
            </tbody>

2

Answers


  1. To display the study program name based on its ID in your Laravel view, you should use the relationship you defined in your model. Since you already have a prodi() relationship in your Mahasiswa model, you can use it to retrieve the study program name. Here’s how you can modify your view:

    Assuming that prodi in your Mahasiswa model represents the foreign key for the study program:

    <tbody>
        @foreach ($mahasiswa as $mhs)
            <tr>
                <td>{{ $loop->iteration }}</td>
                <td>{{ $mhs->nama }}</td>
                <td>{{ $mhs->nim }}</td>
                <td>{{ $mhs->prodi->nama }}</td> <!-- Use the 'nama' attribute of 
    the related Prodi -->
                <td>{{ $mhs->j_kel }}</td>
                <td class="d-flex justify-content-around">
                    <!-- Rest of your code -->
                </td>
            </tr>
        @endforeach
    </tbody>
    

    In this code, $mhs->prodi->nama will fetch the nama attribute from the related Prodi model based on the prodi foreign key.

    Ensure that your prodi relationship in the Mahasiswa model correctly specifies the relationship to the Prodi model, like you’ve shown:

    public function prodi()
    {
        return $this->belongsTo(Prodi::class, 'prodi');
    }
    

    With this change, your table should display the study program name instead of the study program ID.

    Login or Signup to reply.
  2. If you want to display the name of the study program instead of its ID, you only need to make changes in your blade file because you already defined the relationship in your model.

    Instead of,

    <td>{{ $mhs->prodi }}</td>
    

    Write it like this,

    <td>{{ $mhs->prodi->nama }}</td>
    

    You have already defined a relationship in the model, so using the above syntax will return the name using dynamic relationship.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search