Laravel 5.1 - 在表单中循环遍历数据(Blade)

3
我有一个问题,无法显示我的产品可用的颜色,我尝试使用Blade循环来显示它们,但是它不起作用。 我的资源控制器:
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'));     
    }

这是我的桌子颜色,我正确添加了关系 在此输入图片描述 产品型号:
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)显示我产品中所有可用的颜色。
我该如何解决?谢谢你的帮助!
4个回答

3

从我的观察中,您没有向视图传递颜色的集合(数组),而是传递了一个查询构建器。您需要在管道的末尾添加一个查询执行方法,例如 get():

// 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()all()
$colors = Color::where('product_id', $id)->orderBy('id', 'asc')->get();

或者

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接