lundi 5 février 2018

Implementing relationship in models

I have two models: Dish and DishCategory. I decided to implement a "One to many" relationship. Here's a migration for Dish model:

Schema::create('dishes', function (Blueprint $table) {
    $table->increments('id');
    $table->string('dish', 50);
    $table->string('photo');
    $table->double('price', 8, 2);
    $table->integer('category_id');
    $table->integer('type_id'); /* 1 - menu for delivery; 0 - general menu */
});

And a migration for DishCategory model:

Schema::create('dish_categories', function (Blueprint $table) {
    $table->increments('id');
    $table->string('category');
});

I've created a method called dish() in DishCategory model:

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

And dish_category() in Dish model:3

public function dish_category()
{
    return $this->belongsTo('App\DishCategory', 'category_id');
}

I'm trying to set up a foreign key in my relationship, so it's been set up in dish_category() method as a second parameter of belongsTo(). But it doesn't work. What is the workaround?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire