Hello. I have two table, Penyedia (Vendor) and Pekerjaan (Work), I already made the eloquent relationship, Penyedia (vendor) hasMany Pekerjaan (work), and Pekerjaan (work) belongsTo Penyedia (vendor). Here is the model:
Pekerjaan.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Pekerjaan extends Eloquent
{
use HasFactory;
protected $guarded = [];
public function penyedia(){
return $this->belongsTo(Penyedia::class);
}
}
Penyedia.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Penyedia extends Eloquent
{
use HasFactory;
protected $guarded = [];
public $timestamps = false;
public function pekerjaan(){
return $this->hasMany(Pekerjaan::class);
}
}
Now I have create a table to show the list of Penyedia (vendor) and an action button that will redirect to a new page with the list of the Pekerjaan (work) based on the ID of the Penyedia (vendor).
tabelnilai_penyedia.blade.php (show all Penyedia (vendor))
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">Tabel Penilaian Penyedia</h3>
</div>
<!-- /.card-header -->
<div class="card-body table-responsive">
<table id="tabelpenyedia" class="table table-bordered">
<thead>
<tr>
<th style="width: 10px">No.</th>
<th>Nama Penyedia</th>
<th>Nilai</th>
<th style="width: 120px">Aksi</th>
</tr>
</thead>
<tbody>
@php $no = 1; @endphp
@foreach ($penyedia as $penyedias)
<tr>
<td>{{$no++}}</td>
<td>{{$penyedias->nama}}</td>
<td>{{number_format($penyedias->nilai,1)}}</td>
<td>
<a href="/nilaipekerjaan/{{$penyedias->pekerjaan}}" type="button" class="btn btn-primary btn-block btn-outline-primary">Beri Penilaian</a> <!--NOT SURE ABOUT THIS-->
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div>
</section>
tabelnilai_pekerjaan.blade.php (This is the table I want to show based on the ID of the Penyedia (vendor))
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">Tabel Nilai Pekerjaan</h3>
</div>
<!-- /.card-header -->
<div class="card-body table-responsive">
<table id="tabelpekerjaan" class="table table-bordered">
<thead>
<tr>
<th style="width: 10px">No.</th>
<th>Paket Pekerjaan</th>
<th>Nama Perusahaan</th>
<th>Lokasi Pekerjaan</th>
<th>HPS</th>
<th>Nilai Kontrak</th>
<th style="width: 120px">Aksi</th>
</tr>
</thead>
<tbody>
@php $no = 1; @endphp
@foreach ($pekerjaan as $pekerjaans)
<tr>
<td>{{$no++}}</td>
<td>{{$pekerjaans->pekerjaan}}</td>
<td>{{$pekerjaans->penyedia->nama}}</td>
<td>{{$pekerjaans->lokasi}}</td>
<td>Rp. {{number_format($pekerjaans->hps,0,',',',')}}</td>
<td>Rp. {{number_format($pekerjaans->nilai_kontrak,0,',',',')}}</td>
<td>
<a href="#" type="button" class="btn btn-outline-primary">Edit</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div>
</section>
Here is the web.php and AdminController
web.php
Route::get('/datanilai_penyedia',[AdminController::class,'tabelnilai_penyedia'])->name('datanilai_penyedia');
Route::get('/nilaipekerjaan/{id}',[AdminController::class,'showpekerjaan']);
AdminController
public function tabelnilai_penyedia(){
$penyedia = penyedia::all();
return view('admin.datanilai_penyedia', compact('penyedia'));
}
public function showpekerjaan(){
$pekerjaan = Pekerjaan::with('penyedia')->paginate();
return view('admin.showpekerjaan', compact('pekerjaan'));
}
Here is my database screen shot imgur.com/a/ZvM2Ee9
pass parameter id to showpekerjaan(), ex : showpekerjaan($id)
then
$pekerjaan = Pekerjaan::with('penyedia')->whereHas('penyedia',function($q) use($id){ $q->where('foreign_key',$id); })->paginate();
foreign_key -> you foreign key in Pekerjaan entity
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community