Just Another Guy
Just Another Guy

Reputation: 147

Simple laravel models usage

My model:

class Product extends Model
{
    protected $table = 'Products';
    protected $fillable = ['product_code', 'type', 'name', 'description', 'price', 'discount', 'image', 'image_alt'];

    public function products()
    {
        return $this->hasMany('App\ProductSpecifics');
    }
}

My controller code:

public function product($code)
{
    $product = Product::where('product_code',$code)->get();
    $productSpec = ProductSpecifics::where('product_code',$code)->get();
    var_dump($product->name);
    return view('pages.product', compact('product','productSpec'));
}

Error: Property [name] does not exist on this collection instance

I tried using dd($product) and I noticed that there is a lot of information in there.dd response

How do I extract only the attributes like name,type & etc ?

Upvotes: 0

Views: 28

Answers (1)

Kalim
Kalim

Reputation: 499

try this

dd($product->toArray());

Upvotes: 1

Related Questions