Get data from another table matching name laravel
P粉378264633
P粉378264633 2024-01-29 12:28:44
0
1
379

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

P粉378264633
P粉378264633

reply all(1)
P粉710478990

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

{{$penyedias->pekerjaan[0]->pekerjaan}}

To display all jobs, you can loop on $provider->jobs, for example:

@php $no = 1; @endphp
                  @foreach ($penyedia as $penyedias) 
                   @foreach($penyedias->pekerjaan as $pekerjaan)
                  
                    {{$no++}}
                    {{$pekerjaan->pekerjaan}}
                    {{$penyedias->nama}}
                    {{$pekerjaan->lokasi}}
                    Rp. {{number_format($pekerjaan->hps,0,',',',')}}
                    Rp. {{number_format($pekerjaan->nilai_kontrak,0,',',',')}}
                    
                        Edit
                    
                  
                   @endforeach
                  @endforeach

and change $provider =provider::all(); on your controller $provider = Provider::all();

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!