Diego Cespedes
Diego Cespedes

Reputation: 1353

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>{{ $color->color }}</td>
@endforeach

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

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!

Upvotes: 3

Views: 778

Answers (4)

Yasin Patel
Yasin Patel

Reputation: 5731

Add get() Or all() in query:

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

OR

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

Upvotes: 0

Joe
Joe

Reputation: 311

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

Upvotes: 0

Dov Benyomin Sohacheski
Dov Benyomin Sohacheski

Reputation: 7732

You are not running get() on your color query:

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

}

Upvotes: 1

Denis Mysenko
Denis Mysenko

Reputation: 6534

From what I can see, you are not passing a collection (an array) of colours to the view, but a query builder instead. You need to add a query execution method, eg. get() to the end of your pipeline:

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

Upvotes: 3

Related Questions