【问题标题】:Laravel 5.1 - Foreach data in form (Blade)Laravel 5.1 - 表单中的 Foreach 数据(刀片)
【发布时间】:2016-09-19 17:04:22
【问题描述】:

我无法显示我的产品的可用颜色,我尝试使用刀片 foreach 显示它们,但它不起作用。 我的资源控制器:

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 工作正常的产品。

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

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

【问题讨论】:

    标签: php laravel laravel-5 laravel-blade


    【解决方案1】:

    据我所知,您不是将颜色集合(数组)传递给视图,而是将查询构建器传递给视图。您需要添加一个查询执行方法,例如。 get() 到管道的末尾:

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

    【讨论】:

      【解决方案2】:

      您没有在颜色查询中运行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')); 
      
      }
      

      【讨论】:

        【解决方案3】:
        // 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);
        

        【讨论】:

          【解决方案4】:

          在查询中添加 get()all()

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

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

          【讨论】:

            猜你喜欢
            • 2016-10-02
            • 2017-06-20
            • 2018-05-25
            • 2016-06-29
            • 2018-04-04
            • 2021-10-19
            • 1970-01-01
            • 2016-10-23
            • 2019-09-25
            相关资源
            最近更新 更多