Similiar to Post and Category, I have a database tables Penyedia and Pekerjaan. Whereby Penyedia is similiar to Category, and Pekerjaan similiar to Post. Penyedia hasMany Pekerjaans and Pekerjaan belongsTo Penyedia.
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 pekerjaans(){
return $this->hasMany(Pekerjaan::class);
}
}
Here is the Table that shows Penyedia: (There is a button that will redirect the user to show the list of datas Pekerjaan specific to that Penyedia)
<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->id}}" type="button" class="btn btn-primary btn-block btn-outline-primary">Beri Penilaian</a>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div>
</section>
From that table I have a button that will redirect the user to show the list of datas Pekerjaan specific to that Penyedia. Similiar to this tinker:
php artisan tinker
Psy Shell v0.11.5 (PHP 8.1.0 — cli) by Justin Hileman
>>> $a = Penyedia::first();
[!] Aliasing 'Penyedia' to 'App\Models\Penyedia' for this Tinker session.
=> App\Models\Penyedia {#4473
id: 1,
nama: "CV. KONIRISA",
npwp: "01.453.410.1-701.000",
alamat: "Jl. Prof. DR. Hamka No. 29 A Pontianak",
lati: -0.0225449,
longi: 109.324,
nilai: 0.0,
}
>>> $a->pekerjaans
=> Illuminate\Database\Eloquent\Collection {#4722
all: [
App\Models\Pekerjaan {#4720
id: 7,
pekerjaan: "tes",
lokasi: "tes",
hps: 199902994.72,
nilai_kontrak: 199419622.92,
lati: -0.00677621,
longi: 109.375,
gambar: null,
penyedia_id: 1,
nilai: 0.0,
created_at: "2022-06-22 11:12:55",
updated_at: "2022-06-22 12:20:09",
},
App\Models\Pekerjaan {#3790
id: 8,
pekerjaan: "tes",
lokasi: "asdnasjdasd",
hps: 1023123.0,
nilai_kontrak: 12301203.0,
lati: 1231.0,
longi: 123.0,
gambar: null,
penyedia_id: 1,
nilai: 0.0,
created_at: "2022-06-23 00:10:59",
updated_at: "2022-06-23 00:10:59",
],
}
I have tried these two methods below in the AdminController:
//Show all Penyedia from database
public function tabelnilai_penyedia(){
$penyedia = penyedia::all();
return view('admin.datanilai_penyedia', compact('penyedia'));
}
//Get Pekerjaan specific to the Penyedia
public function showpekerjaan(Penyedia $penyedia){
$pekerjaans = Penyedia::where('id', $penyedia->id)->get();
$penyedia = Penyedia::with('pekerjaans')->get();
return view('admin.showpekerjaan', compact('penyedia'))->with('pekerjaans',$pekerjaans);
}
tabelnilai_pekerjaan.php (This is the table that I want to show, it's get data based on the 'Penyedia')
<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>