dimanche 22 mai 2016

Laravel 5.1 - Foreach data in form (Blade)

i have a problem to show colors avaible of my products, i tryed to show them with blade foreach, but it doesnt work. My resource 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')); 

    }

This is my table color, i added correctly the relations enter image description here Product Model:

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

    }

}

Color Model

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

    }
}

I'm trying to show the color avaible for my product so:

    <label for="p_color">Color</label>


     @foreach($colors as $color)
 <td></td>
     @endforeach

This is only test! I would like show a select option, i tryed to use BLADE but it doesnt work,

-get all colors where product_id = $id work fine.

-get the product where id = $id work fine.

-I think the problem is the code blade(foreach) to show all colors avaible for my product.

how can i resolve it? Thank your help!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire