In my blade i use triple foreach in laravel, because i do 3 query for database in controller. And it showed me doubled data and it sort ascending.
here's my code :
1. Blade (with foreach)
@foreach ($keranjang as $purchase)
@foreach ($pelanggan as $member)
@foreach ($produk as $product)
<tr>
<th scope="row">{{ $purchase->ID_pembelian }}</th>
<td>{{ $product->nama_produk }}</td>
<td>{{ $member->nama_pelanggan }}</td>
<td>{{ $purchase->tgl_pembelian }}</td>
<td>{{ $purchase->jumlah }}</td>
<td>{{ $purchase->harga }}</td>
<td>{{ $purchase->total }}</td>
<td>{{ $purchase->status }}</td>
</tr>
@endforeach
@endforeach
@endforeach
2. Controller with 3 queries
public function index()
{
return view('keranjang', [
'keranjang' => $pembelian = Pembelian::where('ID_pelanggan', auth()->user()->ID_pelanggan)->get(),
'pelanggan' => $pelanggan = Pelanggan::where('ID_pelanggan', auth()->user()->ID_pelanggan)->get(),
'produk' => $produk = Produk::find(Pembelian::where('ID_pelanggan', auth()->user()->ID_pelanggan)->get('ID_produk')),
"title" => "Keranjang"
]);
}
. . but, i want it to be like below (not showing double data and not sorting ascending):
And i change the controller and use first(), it showed me the right first data i want which 'kandang kucing' besides 'pet bowl', but both of the value is the same :(.
'produk' => $produk = Produk::find(Pembelian::where('ID_pelanggan', auth()->user()->ID_pelanggan)->get('ID_produk')->first()),
how do i fix it? is it wrong with my controller or my foreach in blade?
CodePudding user response:
Well from your data it seems like you are fetching related data from 3 different tables, what you can do here is make use of one of Laravel's many features, known as Many-to-Many relation that works with pivot tables.
Step 1 - Making Pivot Table
Learn How to make a pivot table
php artisan make:migration create_alpha_beta_table --create --table=alpha_beta
While creating the pivot table we don't need a model so its just a migration with a conventional name of related tables in alphabetical order, for example if the two related tables are menu and restaurant the table will be menu_restaurant
with menu
first and restaurant
second according to their alphabetical order. This can't be restaurant_menu
hence, alpha_beta
not beta_alpha
.
Step 2 - Define Relationships
Once you are finished creating pivot you can simply define relation in each tables Model, for example:
Alpha.php
Model:
public function beta() {
return $this->hasMany(Beta::class);
}
Beta.php
Model
public function alpha(){
return $this->belongsTo(Alpha::class);
}
Step 3 - Use the Relationship
Once you are finished, defining the relationship, you can simply call the relationships using with()
on your query builder and the data will be in a nice and clean format ready to use inside a single loop, Now to call it inside the AlphaController.php
```
Alpha::where('id',1)->with('beta')->get()
```
this will give you following result:
alpha[
id => 1,
name => 'some really alpha name',
another_col => 'data',
beta => [
id => 3,
alpha_id => 1,
name => 'some really beta name',
another_beta_col => 'data'
]
]
See how you have beta combined with alpha where the foreign key is matched.