dptrmt4366 2015-06-24 04:45
浏览 21
已采纳

Laravel在一个产品中显示许多规格

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
  • 写回答

1条回答 默认 最新

  • doulian7252 2015-06-24 13:41
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c