私の製品の色を表示するのに問題があります。ブレード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'));
}
ですが、私は正確に関係 製品モデルを追加しました私の製品の色を表示する:
<label for="p_color">Color</label>
@foreach($colors as $color)
<td>{{ $color->color }}</td>
@endforeach
これはテストのみです!私は選択オプションを表示したい、私はブレードを使用しようとしましたが、それはdoesnt仕事、
- product_id = $ idがうまく動作するすべての色を取得します。
- id = $ idがうまく動作する製品を入手してください。
問題は、私の製品にすべての色を表示するコードブレード(foreach)だと思います。
どうすれば解決できますか?ありがとうございました!