gpt4 book ai didi

php - Laravel 5.1 - 表单中的 Foreach 数据(Blade)

转载 作者:可可西里 更新时间:2023-11-01 00:57:26 24 4
gpt4 key购买 nike

我在显示我的产品的可用颜色时遇到问题,我尝试用 blade foreach 来显示它们,但它不起作用。我的资源 Controller :

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产品型号:

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');
}
}

我正在尝试显示我的产品可用的颜色:

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

这只是测试!我想显示一个选择选项,我尝试使用 BLADE 但它不起作用,

  • 获取 product_id = $id 工作正常的所有颜色。
  • 获取 id = $id 工作正常的产品。

我认为问题出在显示我的产品可用的所有颜色的代码 Blade (foreach)。

我该如何解决?谢谢你的帮助!

最佳答案

据我所知,您没有将颜色集合(数组)传递给 View ,而是传递给查询生成器。您需要添加查询执行方法,例如。 get() 到管道的末尾:

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

关于php - Laravel 5.1 - 表单中的 Foreach 数据(Blade),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37380442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com