Associative table in Laravel 5.8 (Migrate)

Asked

Viewed 173 times

-1

I am making a system for a school in the automotive sector that offers courses, this system the user will be able to register the courses and view them, in general, will be a panel for him to control what he puts/ remove from the site.

Throughout the development of the system there were some doubts in the Laravel Migrations, I tried to find in other sources, but I did not get any common result to mine, amazingly, most start using the command php artisan make:model nome_da_tabela -m (-m to generate migrate), but I ended up doing the opposite, I started by migrates to then generate all the tables at once.

I have 3 Migrations/tables, but I don’t know what the final result of the associative table will look like, I mean regarding the types of data and the references I should put in the table course_category

create_course_table

inserir a descrição da imagem aqui

create_course_category_table

inserir a descrição da imagem aqui

create_category_course_table

inserir a descrição da imagem aqui

On the table course_category how I will insert and identify the table id course and category_course?

I thought I’d put as FK the two of the table category_course, but I do not know if it is feasible knowing that it is a table of N:N

Example:

$table->foreign('id_course')->references('id')->on('course')

$table->foreign('id_category_course')->references('id')->on('category_course')

However this form quoted above may be quite wrong working with Laravel. I do not know, I have no way to state.

This question may be duplicated in Migrations in Laravel 5.8 but it was not answered...

  • 1

    You need to know how to create a Migration?

  • Actually my question is whether in Migration would the associative table or not, from what I saw, will not... You use the pivot command to retrieve the data from the intermediate table, I just didn’t understand how to insert it.

  • 1

    But there needs to be a table and consequently a migrate from these tables, summarizing are 3 Migration

  • But from what I understood (correct me if I’m wrong) it will be 2 tables... Course and category_course that will exist in my models folder, and if I want to get the information of the 2 tables I must use the pivot command. If not that, how will I do when mounting my course_category migrate? will have migrate id, and the keys referenced, should I use some Laravel 5 command to reference them? Type $table->Foreign('id_course')->References('id')->('id_course');?

  • There will be three Migration, one course, the other category and the other category? that’s what you need?

  • Oh yes! I get it. But it would be right if I put. $table->BigIncrements('id'); $table->foreign('id_course')->references('id')->on('course'); $table->foreign('id_category_course')->references('id')->on('category_course'); ?

  • You need to create 3 migrate one for each table and in Laravel are created 2 Models that relate many to many... !!! understood?

  • 1
  • Okay! Sorry to ignore, you’ve helped a lot.

Show 4 more comments

1 answer

0

Imagining that you are already passing the course categories to your view, on your form you must have a select for these categories:

<select name="categories_id[]" multiple>
    @foreach($categories as $category)
        <option value="{$category->id}">{$category->name}</option>
    @endforeach
</select>

After sending the form you should receive the data in this way in your controller:

$data = $request->all();

// Sync recdebe o array com os ID's das categorias selecionadas e as insere na sua tabela intermediária
$course->categories()
    ->sync($data['categories_id']);

Behold: Relations N:N

Browser other questions tagged

You are not signed in. Login or sign up in order to post.