0

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>

Database enter image description here

enter image description here

YuPi
  • 75
  • 8

1 Answers1

0

Assumption : You want to find all pekerjaans assosicated to penyedia and you get the correct id of penyedia in $penyedia->id

Hope this help you

inside AdminController:

  public function showpekerjaan(Penyedia $penyedia){

    $pekerjaan = Penyedia::with('pekerjaans')->where('id',$penyedia->id)->get()

    return view('admin.showpekerjaan',['pekerjaan' => $pekerjaan]);
}

for view you can put id of penyedia like:

<a href="{{ url('getPekerjan/') . $penyedias->id  }}" type="button" class="btn btn-primary btn-block btn-outline-primary">Beri Penilaian</a>
Bhimani Rutvik
  • 398
  • 2
  • 13
  • May I know how should I put it to ? Thank you in advance @Bhimani – YuPi Jun 23 '22 at 06:57
  • The return have a problem, syntax error, may I know how to resolve this? @Bhimani – YuPi Jun 23 '22 at 07:04
  • ` public function showpekerjaan(Penyedia $penyedia){ $pekerjaan = Penyedia::with('pekerjaans')->where('id',$penyedia->id)->get(); return view('admin.showpekerjaan', compact('penyedia'))->with('pekerjaan', $pekerjaan); }` << Updated AdminController for showpekerjaan, still don't know how to put it into – YuPi Jun 23 '22 at 07:20
  • hope this might help you : https://stackoverflow.com/questions/33266316/laravel-5-getting-id-from-url – Bhimani Rutvik Jun 23 '22 at 07:21
  • It is now showing me a blank table... Can you see my edited post, I've included the table that I want to show according to the 'Penyedia' @Bhimani – YuPi Jun 23 '22 at 07:25