2016-05-22 3 views
3

私の製品の色を表示するのに問題があります。ブレードforeachで表示しようとしましたが、動作しません。 マイリソースコントローラ:Laravel 5.1 - 形式でのForeachデータ

namespace dixard; 

use Illuminate\Database\Eloquent\Model; 
use dixard\User; 
use dixard\Category; 
use dixard\Gender; 
use dixard\OrderItem; 
use dixard\Color; 

class Product extends Model 
{ 
    protected $table = 'products'; 
    protected $fillable = 
    [ 
     'name', 
     'slug', 
     'description', 
     'extract', 
     'image', 
     'visible', 
     'price', 
     'category_id', 
     'gender_id', 
     'user_id' 
    ]; 

    // Colleghiamo OGNI prodotto ha un utente 
    public function user() { 
     return $this->belongsTo('dixard\User'); 
    } 

    // Colleghiamo OGNI prodotto ha una categoria 
    public function category() { 
     return $this->belongsTo('dixard\Category'); 
    } 

    public function gender() { 
     return $this->belongsTo('dixard\Gender'); 
    } 

    // prodotto è contenuto in TANTI order item 
    public function OrderItem() { 
     return $this->belongsTo('dixard\OrderItem'); 
    } 

    // prodotto è contenuto in TANTI order item 
    public function Color() { 
     return $this->belongsTo('dixard\Color'); 
    } 
} 

カラーモデル

namespace dixard; 

use Illuminate\Database\Eloquent\Model; 

class Color extends Model 
{ 
    protected $table = 'colors'; 

    // gli dico che voglio scrivere questo campi 
    protected $fillable = [ 
     'color', 
     'product_id', 
    ]; 

    public $timestamps = false; 

    // Ogni color HA tanti prodotti. // Ogni prodotto ha tanti colori 
    public function products() { 
     return $this->hasMany('dixard\Product'); 
    } 
} 

私がしようとしている:これは私のテーブルの色が

public function show($id){ 
     $colors = Color::where('product_id', $id)->orderBy('id', 'asc'); 
     $product = Product::where('id', $id)->first(); 

     return view('store.show', compact('product','colors'));  
    } 

ですが、私は正確に関係 enter image description here 製品モデルを追加しました私の製品の色を表示する:

<label for="p_color">Color</label> 
@foreach($colors as $color) 
    <td>{{ $color->color }}</td> 
@endforeach 

これはテストのみです!私は選択オプションを表示したい、私はブレードを使用しようとしましたが、それはdoesnt仕事、

  • product_id = $ idがうまく動作するすべての色を取得します。
  • id = $ idがうまく動作する製品を入手してください。

問題は、私の製品にすべての色を表示するコードブレード(foreach)だと思います。

どうすれば解決できますか?ありがとうございました!

答えて

3

私が見るところでは、コレクションの色(配列)をビューに渡すのではなく、クエリビルダを渡しています。クエリ実行メソッドを追加する必要があります。あなたのパイプラインの最後に()を取得する:

// Adding get() will execute this query 
$colors = Color::where('product_id', $id)->orderBy('id', 'asc')->get(); 
1

あなたの色のクエリにget()を実行していない:

public function show($id){ 

    $colors = Color::where('product_id', $id) 
        ->orderBy('id', 'asc') 
        ->get();   // <-- Add this 

    $product = Product::where('id', $id)->first(); 

    return view('store.show', compact('product','colors')); 

} 
0
// To select multi records,you have to use get(); 

$colors = Color::where('product_id', $id)->orderBy('id', 'asc') 
      ->get(); // add get() 

// To select single record, you have to use first(); 
$product = Product::where('id', $id)->first(); 

// By the way, you can also use this statement to replace the second statement. 
// This only worked for primary key and the key must be id) 
$product = Product::find($id); 
0

のget()またはすべてを(追加)にクエリ:

$colors = Color::where('product_id', $id)->orderBy('id', 'asc')->get(); 

または

$colors = Color::where('product_id', $id)->orderBy('id', 'asc')->all();