Before we start, some translations since my code is not in English:
provider = supplier
job=work
So I have two tables, supplier table and work table. Each supplier can own multiple works, and each work can only be owned by one supplier. It should be a one-to-many relationship. This is my table
Supplier table
Workbench
So, I use LARAVEL as my framework. I have created a vendor table with an action button and I want it to work and if the button is clicked it will show a list of jobs (pekerjaan) owned by the exact vendor name. As you can see from the table above, I added the vendor name (nama) to a "sheet" in the database. How can I make this feature work? I'm new to Eloquent in Laravel.
This is my model file:
Jobs.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('App\Models\Penyedia'); } }
Provider.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('App\Models\Pekerjaan'); } }
AdminController.php
public function showpekerjaan(){ $penyedia = penyedia::all(); return view('admin.showpekerjaan', compact('penyedia')); }
tabelnilai_pekerjaan.blade.php(I want to display this sheet based on the supplier)
<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 ($penyedia as $penyedias) <tr> <td>{{$no++}}</td> <td>{{$penyedias->pekerjaan->pekerjaan}}</td> <td>{{$penyedias->pekerjaan->nama}}</td> <td>{{$penyedias->pekerjaan->lokasi}}</td> <td>Rp. {{number_format($pekerjaans->pekerjaan->hps,0,',',',')}}</td> <td>Rp. {{number_format($pekerjaans->pekerjaan->nilai_kontrak,0,',',',')}}</td> <td> <a href="#" type="button" class="btn btn-primary btn-block btn-outline-primary">Edit</a> </td> </tr> @endforeach </tbody> </table> </div> <!-- /.card-body --> </div> <!-- /.card --> </div> </section>
Is there anything I need to add, edit or change? Thanks
You are trying to print an array as a string. Your pekerjaan() relation has hasmany property and it returns array. If you want to display all pekerjaan, you need to run a second loop inside the loop. If you only need to display one, you can simply do this
To display all jobs, you can loop on $provider->jobs, for example:
and change $provider =provider::all(); on your controller $provider = Provider::all();