I followed a tutorial about many-to-many relationships with Laravel (7 in my case).
The result is good, I learned a lot, but what I find strange is that I do not have physical relationships between the different tables.
I created a relationship many to many, which should link 3 tables, products, categories and products_categories
My questions :
1- Is it essential to have a physical relationship in the schema of the database?
2- How can I make these relationships appear in my diagram?
Here is a current photo of the database schema :
In this database, I have links between tables :
The Laravel relationships are not the same as your database relationships (MySQL, or others).
You don't need to have a database relationship to have your application working. it is really depending on what you are trying to achieve.
If you want to see the relationships between your tables, make sure to specify the foreign keys in your migration Schema (https://laravel.com/docs/7.x/migrations#foreign-key-constraints) such as:
Schema::table('posts', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
For pivot tables you can also use a migration Schema as follow:
Schema::table('category_product', function (Blueprint $table) {
$table->unsignedBigInteger('category_id');
$table->unsignedBigInteger('product_id');
$table->foreign('category_id')->references('id')->on('categories');
$table->foreign('product_id')->references('id')->on('products');
});
Related
I have a migration with three tables and i have a question which decide the how the tables will connect to each other and do i really a foreign key on them so what i want to do I have two roles 1 admin - 2 user and i want to assign one of them to each user because i want only admin users to login into backend dashboard as for now i make the relationship between roles and users as one(role)-to-many(users) Also the relationship between posts and users is one(user)-to-many(posts) since the post in only related to one user only but the user can have multiple posts so i need you help here to understand those thing in better way and when i need to setup a Foreign Key in the table.
This is my tables:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('fullname');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password');
$table->integer('role_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles');
$table->rememberToken();
$table->timestamps();
});
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->string('content');
$table->string('image');
$table->timestamps();
});
From what I am understanding, you have three tables: roles, users, and posts.
Whenever you have a relationship like a one-to-many, the table corresponding to the many side will contain the foreign key to the table corresponding to the one side. You do not put any foreign key in the other table.
Now the roles and user table share a one-to-many relationship respectively, thus you put the role_id in the user table and do not put any foreign key in the roles table. Similarly, the posts table is on the many side of the relationship (one user can have MANY posts) so you have to put user_id in the posts table but no foreign key in the user table.
After you do create the migrations, to be able to query in between the tables, you need to define model files. These model files will contain methods used to retrieve associated records. For a one-to-many relationship, you will be required to have two models (for both tables) and in those models use functions as: belongsTo for the table with the foreign key, and a hasMany function for the table without the foreign key. For your case, the user model will have a hasMany function for the posts model, and a belongsTo function for the roles model.
Hope that clears it up
I have two database and inside each I have one table:
database_one -> one (table)
database_two -> two (table)
I would like to create a relationship Many-To-Many between two tables inside database_two; but the tables are in two different database.
This is my code for create a pivot table on the same database:
Schema::connection('database_two')->create('one_two', function (Blueprint $table) {
$table->integer('one_id')->unsigned()->nullable();
$table->foreign('one_id')->references('id')
->on('one')->onDelete('cascade');
$table->integer('two_id')->unsigned()->nullable();
$table->foreign('two_id')->references('id')
->on('two')->onDelete('cascade');
$table->timestamps();
});
How can I do?
Thanks!
laravel does not support relations across two databases:-)
I have a problem with delete using laravel 5.4
I have 3 tables: Users, Posts, Vechicles
User hasMany Posts
Vehicle belognsTo Post
Post hasMany Vehicles
...anyway...when creating the schema for the vehicle table, i use 2 foreign keys:
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->foreign('post_user_id')->references('user_id')->on('posts');
When i want to delete a post, all the vehicles that are related to the post to be deleted....but is not working (it gives an error about constraint)
Can someone tell me what i am wrong? it is that i am using 2 foregin keys?
check this
$table->engine = "InnoDB";
Using one foreign key solve the issue:
Schema::create('vehicles', function (Blueprint $table) {
$table->increments('id');
$table->string('mark');
$table->string('model');
$table->integer('weight');
$table->string('vehicle_state');
$table->integer('post_id')->unsigned();
$table->integer('post_user_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
});
I have a question that I'm not sure how to solve or even phrase for finding an answer.
I have a Company Model & User Model that are related Many-to-Many.
I want to have a user_pins table. A user can belong to multiple companies and therefore have a different pin within each company. The pin is unique within a company, no two users within a company can have the same one. Users in different companies can have the same one.
So for the company it is One to Many, for the user it is One to Many, but altogether it is many to many, Im not sure if that makes sense.
I have the table set up as
Schema::create('user_pins', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('company_id')->unsigned();
$table->string('pin');
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onUpdate('cascade')->onDelete('cascade');
$table->primary(['user_id', 'company_id', 'pin']);
});
How do I relate this table in the models and use Eloquent to access/create/update it so it stores both the user and company?
Firstly, I would change the name to company_user so that it follows the same naming convention that Laravel would use out of the box. (you wouldn't have to do this as you can specify the pivot table name in the relationship but if there isn't a reason to stick with user_pin it makes sence to follow convention :) )
Then I would remove the primary key from being a compound of all 3 fields and just have it on the company_id and user_id.
Lastly, as a PIN only has to be unique for a company, I would just put the unique index on those two columns e.g.
Schema::create('company_user', function (Blueprint $table) {
$table->integer('company_id')->unsigned()->index();
$table->integer('user_id')->unsigned()->index();
$table->string('pin');
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
$table->foreign('company_id')->references('id')->on('companies')->onUpdate('cascade')->onDelete('cascade');
$table->primary(['company_id', 'user_id']);
$table->unique(['company_id', 'pin']);
});
Then for the relationship in the model I would have something like:
return $this->belongsToMany('App\Company')->withPivot('pin');
and
return $this->belongsToMany('App\User')->withPivot('pin');
Examples of use with pivot
All user pins for a company:
$company->users->lists('pivot.pin');
Users pin for a specific company
$user->companies()->where('id', $id)->get()->pivot->pin;
Users pin for the first company relationship:
$user->companies->first()->pivot->pin;
Hope this helps!
I have a situation in Laravel 5.1 where I would like to add a many-to-many relationship to an existing relationship. According to the diagram below, I already have all the items in green working.
The issue is that since there isn't a primary key on the issue_person table, I don't know how to add a many-to-many relationship to Users. Does anyone know how I would go about accomplishing this?
So it appears that a simple answer to this is to write a migration that adds a primary key to the original issue_person pivot table, and then set up a many-to-many relationship between issue_person and user using the position_user table.
My migration looks like this:
public function up()
{
Schema::table('issue_person', function (Blueprint $table) {
$table->increments('id');
});
}
public function down()
{
Schema::table('issue_person', function (Blueprint $table) {
$table->dropColumn('id');
});
}