jeudi 30 janvier 2020

Get attribute from selected model

I am using PHP 7.1.33 and Laravel Framework 5.8.36.

I am getting a model back by name like the following Product::where('name', '=', $name)->get();

My Product model looks like the following:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'product';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'url', 'category'];
}

When trying to get an attribute like the following I get an error:

$product = Product::where('name', '=', $name)->get();
$product->$url; // ERROR
$product->$category; // ERROR

Any suggestions how to access the attributes on my retrieved model?

I appreciate your replies!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire