1

I just started to work on a laravel project for my school assignment. I just have started it for about a week so my fundamental knowledge about laravel is not complete.

Today I bump into a problem with model many to many relationship in laravel. I create 2 model with migration A and B. In App\A.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class A extends Model
{
    //
    public function B(){
        return $this->belongsToMany('App\B');
    }
}

and in App\B.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class B extends Model
{
    //
    public function A(){
        return $this->belongsToMany('App\A');
    }
}

I think it should do the job. But when I use seeder to create dummy data, I got the error with is that table A_B is not created. I assume that I must create empty table A_B for 2 pivot columns which is annoying. Is there a better way, a proper way to create many to many relationship without manually create pivot table for them?

Silver
  • 29
  • 1
  • 9

1 Answers1

0

I'm afraid not. There are shortcuts to doing it, like the way they show here but you will end up doing some manual work to create the table. You will end up using a migration anyway, but depending on the amount of control you want with the pivot, you might want to use a model for that. Using a model for a pivot table is not mandatory.

https://laravel.com/docs/5.5/migrations#creating-tables

This could be what your migration would end up looking like:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateABTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('A_B', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('a_id')->unsigned();
            $table->integer('b_id')->unsigned();
            $table->foreign('a_id')->references('id')->on('a')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('A_B');
    }
}

But if you really wanted to, you could also create a custom table (with a custom name like ABRandomName) using a new model. Just look for the Defining Custom Intermediate Table Models header in the documentation:

https://laravel.com/docs/5.5/eloquent-relationships#many-to-many

  • so, there is no way for laravel to create that pivot table automatically? And I have to create a miration or a pivot model? – Silver Jan 03 '18 at 16:50
  • I'm afraid not. There are shortcuts to doing it, like the way they show [here](https://stackoverflow.com/a/15834061/6903994), but you will end up doing some manual work to create the table. You will end up using a migration anyway, but depending on the amount of control you want with the pivot, you might want to use a model for that. Using a model for a pivot table is not mandatory. – Brandon Oldenhof Jan 04 '18 at 19:04
  • Oh, that what I looking for, thank you for link me to that post – Silver Jan 05 '18 at 08:49
  • You're very welcome! Please don't forget to mark my answer as the solution if it helped :) – Brandon Oldenhof Jan 07 '18 at 15:00