How to relate from many to many in Eloquent (Laravel)

0

I am using MySQL as a database and Eloquent as ORM.

I have been creating a page that is of food or dishes, in which I already have related the meals with the chef that prepare it, so I have them :

Table foods (meals)

    public function up()
{
    Schema::create('foods', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title', 50);
        $table->integer('creator_id')->unsigned();
        $table->string('image', 300);
        $table->text('description');
        $table->enum('type', ['breakfast', 'lauch', 'dinner', 'snack']);
        $table->enum('status', ['active', 'inactive']);
        $table->timestamps();

        $table->foreign('creator_id')->references('id')->on('creators');
    });
}

Table menus

 public function up()
{
    Schema::create('menus', function (Blueprint $table) {
        $table->increments('id');
        $table->enum('dia_semana', [
            'lunes',
            'martes',
            'miercoles',
            'jueves',
            'viernes',
            'sabado',
            'domingo',
        ]);
        $table->timestamps();
    });
}

What I want is to relate these two tables so that something like this remains: 3 foods would be in menu , but those 3 meals that are in that menu can be in several menus of the day. Example:

food1, food2, food3 would be in the menu1 which are mondays , another row: food1, food4, food5 which repeats the food1 would be from the menu2 of the days Tuesday and so on.

It has not yet occurred to me how to do it, so I need some help on how I could make that relationship in the alternating table from many to many food_menu .

    
asked by Asdrubal Hernandez 16.07.2018 в 21:55
source

1 answer

0

What I really need to understand is to create an intermediate table to be able to make a N to N relationship between Food and Menu therefore a Meal can be in several Menus and a Menu can have many meals

This would generate a row for each relationship but it fulfills the function you want to achieve

This you can do it in the following way:

public function up()
{
    Schema::create('food_menu', function (Blueprint $table) {
        $table->unsignedInteger('id_food');
        $table->unsignedInteger('id_menu');
        $table->primary(["id_food","id_menu"]);
        $table->timestamps();
    });
} 

Then in your Food and Menu models you make the relationship between them:

class Menu extends Model

    public function foods()
    {
        return $this->belongsToMany(Food::class, 'food_menu', 'id_menu', 'id_food')->withTimestamps();
    }

    // todo lo demas
}

class Food extends Model

    public function menus()
    {
        return $this->belongsToMany(Menu::class, 'food_menu', 'id_food', 'id_menu')->withTimestamps();
    }

     // todo lo demas
}

Then finally if you want to get all the meals that belong to a menu, let's say the one of id 10. You can do it in the following way.

//foods es el nombre del metodo en el modelo Menu que arma la relacion
$comidas = Menu::find(10)->foods; 

I hope it can help you

Greetings!

    
answered by 16.07.2018 / 22:53
source