Reputation: 1027
i have problem one produk have many `spesifikasi' something like this example
Produk 1
Spek a
Spek b
Spek c
Produk 2
Spek x
Spek y
with this code
my controller
public function getDashboard() {
$produk = Produk::all();
$spesifikasi = SpesifikasiProduk::all()->take(2);
return View::make('admin-page.admin-produk')
-> with('produk', $produk )
-> with('spesifikasi', $spesifikasi);
}
my view
@foreach ( $produk as $produk )
<tr>
<td>{{ $produk['nama_produk']}}</td>
<td>{{ substr($produk['deskripsi'], 0, 130).' . . .' }}</td>
<td>
@foreach ( $spesifikasi as $spek )
{{ $spek['title_spek'] }} : {{ $spek['spek'] }} ||
@endforeach
</td>
<td><img src="../{{ $produk['display_thumb'] }}"></td>
i get wrong result
it's become something like this
Produk 1
Spek a
Spek b
Spek c
Spek x
Spek y
Produk 2
Spek a
Spek b
Spek c
Spek x
Spek y
i get a litle clue where i missing
in foreach spesifikasi (view) i should give some condition
but i don't know how to fix it
my database structure is like this
produk { id_produk, nama_produk, deskripsi, dispaly_thumb, created_at, updated_at}
produk_spek { id_spek, id_produk, title_spek, spek, created_at, updated_at }
update Armen Markossyan
this is my model
Produk.php
<?php
class Produk extends Eloquent {
protected $table = 'produk';
protected $primaryKey = 'id_produk';
protected $fillable = array('id_produk', 'nama_produk', 'deskripsi', 'display_thumb');
public function SpesifikasiProduk() {
return $this->hasMany( new SpesifikasiProduk, 'id_produk');
}
public function ImageProduk() {
return $this->hasMany(new ImageProduk, 'id_produk');
}
}
SpesifikasiProduk.php
<?php
class SpesifikasiProduk extends Eloquent {
protected $table = 'produk_spek';
protected $primaryKey = 'id_spek';
protected $fillable = array('id_produk', 'title_spek', 'spek');
public function produk() {
return $this->belongsTo('Produk');
}
}
and i already change to become like this
My Controller
public function getDashboard() {
$produk = Produk::with('SpesifikasiProduk')->all();
return View::make('admin-page.admin-produk')
-> with('produk', $produk );
}
My View
@foreach ( $produk as $produk )
<tr>
<td>{{ $produk['nama_produk'] }}</td>
<td>{{ substr($produk['deskripsi'], 0, 130).' . . .' }}</td>
<td>
@foreach ( $produk->SpesifikasiProduk as $spek )
{{ $spek['title_spek'] }} : {{ $spek['spek'] }} ||
@endforeach
</td>
@endforeach
but i still get error like this
Call to undefined method Illuminate\Database\Query\Builder::all()
what i'm missing?
extended *possible create new thread
public function SpesifikasiProduk() {
return $this->hasMany( new SpesifikasiProduk, 'id_produk')->take(2);
}
let say produk A has 4 spesification
produk B has 3 spesification
produk Chas 5 spesification
with above code, it's give result something like this
Produk A
- spek 1
- spek 2
Produk B
produk C
if i change take
value to 6
the result is
Produk A
- spek 1
- spek 2
- spek 3
- spek 4
Produk B
- spek 1
- spek 2
produk C
Upvotes: 0
Views: 455
Reputation: 1214
You have to read about relationships: http://laravel.com/docs/master/eloquent-relationships#defining-relationships
Read your own words: "one produk have many spesifikasi". This means that you can establish a "hasMany" relationship between Produk
and SpesifikasiProduk
.
Do it like follows:
// Produk model
<?php
namespace App\Models;
class Produk extends \Eloquent {
public function spesifikasi()
{
return $this->hasMany(new SpesifikasiProduk, 'id_produk');
}
}
Your controller code:
public function getDashboard() {
$produk = Produk::with('spesifikasi')->get();
return View::make('admin-page.admin-produk')
-> with('produk', $produk);
}
Your view:
@foreach ( $produk as $produk )
<tr>
<td>{{ $produk['nama_produk']}}</td>
<td>{{ substr($produk['deskripsi'], 0, 130).' . . .' }}</td>
<td>
@foreach ( $product->spesifikasi as $spek )
{{ $spek['title_spek'] }} : {{ $spek['spek'] }} ||
@endforeach
</td>
<td><img src="../{{ $produk['display_thumb'] }}"></td>
@foreach
Good luck!
UPD:
There are many ways to perform what you want.
The easiest way is to use @for
instead of @foreach
:
@for ($i = 0; $i < 2; $i++)
{{ $product->spesifikasi[$i]['title_spek'] }} : {{ $product->spesifikasi[$i]['spek'] }} ||
@endfor
Of course, you have to remove take(2)
from the model.
Upvotes: 1