I think you should create a models and then specify relationships there
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.
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
}
}
Please or to participate in this conversation.