dimanche 1 décembre 2019

Relations in Laravel equivalent

I am beginner in Laravel. I use in my project Laravel 5.8.

I have this code:

class Dish extends Model
{
    protected $quarded = ['id'];
    protected $fillable = ['company_id', 'name', 'description',  'enable'];
    public $timestamps = false;

    public function components()
    {
        return $this->hasManyThrough('App\DishValues', 'App\Dish', 'id', 'dishes_id');
    }
}

class DishValues extends Model
{
    protected $quarded = ['id'];
    protected $fillable = ['dishes_id', 'food_ingredient_id', 'quantity'];
    public $timestamps = false;

    public function ingredient()
    {
        return $this->belongsTo('App\FoodIngredient', 'food_ingredient_id');
    }
}

class FoodIngredient extends Model
{
    use scopeActiveTrait;

    public function scopeVisibleDemo($query)
    {
        return $query->where('available_in_demo', 1);
    }

    protected $quarded = ['id'];
    protected $fillable = ['company_id', 'name', 'garbage', 'energy_value', 'protein', 'fat', 'available_carbohydrates', 'roughage', 'description', 'url_address', 'allergen', 'available_in_demo', 'enable'];
    public $timestamps = false;
}

I get my data:

Dish::with('components')->paginate(25);

How can I get in this code values from FoodIngredient?

This is not working:

Dish::with('components, ingredient')->paginate(25);

or

Dish::with('components')->with('ingredient')->paginate(25);


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire