![](/img/trans.png)
[英]how to get same foreign_key record from multiple row in codeigniter
[英]How to show/get all data by the foreign_key
类似于 Post 和 Category,我有一个数据库表 Penyedia 和 Pekerjaan。 其中 Penyedia 类似于 Category,而 Pekerjaan 类似于 Post。 Penyedia 有许多 Pekerjaan,Pekerjaan 属于 Penyedia。
这是模型:
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);
}
}
这是显示 Penyedia 的表格:(有一个按钮将重定向用户以显示特定于该 Penyedia 的数据列表 Pekerjaan)
<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>
从该表中,我有一个按钮,它将重定向用户以显示特定于该 Penyedia 的数据列表 Pekerjaan。 类似于这个修补匠:
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",
],
}
我在 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 (这是我要展示的表格,它是根据“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>
假设:您想找到与 penyedia 相关的所有 pekerjaans,并且您在 $penyedia->id 中获得 penyedia 的正确 id
希望这对你有帮助
在 AdminController 内部:
public function showpekerjaan(Penyedia $penyedia){
$pekerjaan = Penyedia::with('pekerjaans')->where('id',$penyedia->id)->get()
return view('admin.showpekerjaan',['pekerjaan' => $pekerjaan]);
}
为了查看,你可以把 penyedia 的 id 像:
<a href="{{ url('getPekerjan/') . $penyedias->id }}" type="button" class="btn btn-primary btn-block btn-outline-primary">Beri Penilaian</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.