Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jackFlick's avatar

Laravel - Many To Many Relationship

I would seek like out help with creating Many to Many relationships for my API.

So I have an applications table and users table.

I created a Pivot table named "app_tags" as I want to tag a user with many applications (inverse)

Here's my migration for Applications.

Schema::create('applications', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('application_name');
            $table->text('description');
            $table->timestamps();
        });

And here's for my users migration.

Schema::create('app_tags', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('application_id');
            $table->timestamps();
            
            Schema::create('users', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('name');
                $table->string('email')->unique();
                $table->timestamp('email_verified_at')->nullable();
                $table->string('password');
                $table->rememberToken();
                $table->timestamps();
            });
            $table->unique(['user_id', 'application_id']);

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('application_id')->references('id')->on('applications')->onDelete('cascade');
        });

The migration went fine but when I'm trying to show all the Users applications and inverse using Tinker I get an error

>>> App\User::find(1)->applications;
Illuminate/Database/QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel7.application_user' doesn't exist (SQL: select `applications`.*, 
`application_user`.`user_id` as `pivot_user_id`, `application_user`.`application_id` as `pivot_application_id` from `applications` inner join `application_user` on `applications`.`id` = `application_user`.`application_id` where `application_user`.`user_id` = 1)' 

I've been working on this one for a long time now and can't figure out how to fix it. I'm just new to laravel and really wanted to learn more day by day. I hope you can help me.

0 likes
5 replies
Dooshan's avatar

I think you should create a models and then specify relationships there

MichalOravec's avatar
Level 75

If your pivot table is not application_user you have to define it in the second parameter as app_tags

Documentation: https://laravel.com/docs/7.x/eloquent-relationships#many-to-many

Schema::create('applications', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('application_name');
    $table->text('description');
    $table->timestamps();
});
Schema::create('users', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});
Schema::create('app_tags', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('user_id');
    $table->unsignedBigInteger('application_id');
    $table->timestamps();

    $table->unique(['user_id', 'application_id']);
    
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    $table->foreign('application_id')->references('id')->on('applications')->onDelete('cascade');
});

Application model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Application extends Model
{
    /**
     * The roles that belong to the user.
     */
    public function users()
    {
        return $this->belongsToMany('App\User', 'app_tags'); // this is important
    }
}

User model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The roles that belong to the user.
     */
    public function applications()
    {
        return $this->belongsToMany('App\Application', 'app_tags'); // this is important
    }
}
1 like
jackFlick's avatar

Thank you @michaloravec

That totally works for me as well as this one.

User Model

public function applications()
    {
        return $this->belongsToMany(Application::class, 'app_tags', 'user_id', 'application_id'); 
    }

Application Model

public function users()
    {
        return $this->belongsToMany(User::class, 'app_tags', 'user_id', 'application_id');
    }

Appreciate the help :)

MichalOravec's avatar

@jvbalcita You don't have to define user_id and application_id there in this case. And it's great when you mark your own answer as best answer.

jackFlick's avatar

Hi @michaloravec,

Great! Thank you for that.

Sorry I must have clicked the best answer. I already clicked the best answer on your response awhile ago.

:)

Please or to participate in this conversation.