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:-)
Related
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');
});
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');
});
}
I'm trying to build some relationships in Laravel, i'm a little confused about relationships and migrations. Here is a simple example of what i'm doing:
Users -> has_many -> Cats
So my Users migration file has a relationship to Cats like so:
$table->foreign('cats_id')->references('id')->on('cats')
But when I run my migration, I get:
Error: relation cats does not exist...
Do I need to build the Cats table before the Users table?
Do I also need to specify the foreign relation between the two, or if the models contain "hasMany" and "belongsTo" wouldn't Laravel build those relationships automatically on migration?
Do I actually need migrations?
You can't reference a table that not exists. It has nothing to do with Laravel or Eloquent, it's (My)SQL thing.
First create the parent table users, then the child table cats referencing the first:
$table->foreign('user_id')->references('id')->on('users')
this is how User hasMany Cat would look like. cats table has foreign key referencing users table, not the other way around like you tried.
You need to set the foreign key to the table where the 'many' are.
$table->foreign('id')->references('cats_id')->on('Users')
You need to make sure that
Table 'Users' exists before you create table Cats (Or any other table that is referenced)
Column 'id' exists before you create the foreign key. (Or any other column that is referenced)
A quite bulletproof solution for me is to setup the tables with a first migration eg
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->timestamps();
$table->string('cats_id');
});
//and
Schema::create('cats', function(Blueprint $table)
{
$table->increments('id');
$table->timestamps();
$table->string('cat_name');
});
}
in one migration file and then I create another migration file that runs in the end and creates all the foreign keys for all migrations that were running before:
public function up()
{
Schema::table('cats', function(Blueprint $table)
{
$table->foreign('id')->references('cats_id')->on('users');
});
}
You can also choose what should happen to your cats table on update or delete of a user by adding eg
->onUpdate('CASCADE')->onDelete('CASCADE');
to the $table->... line
You will have to run the migration for cats table and create that table before you can associate it with users table.
Foreign key relation will be helpful when you are required to do cascade delete or update. Also an insert like the following will be easier for you with the cats relationship set.
$user = User::find(1);
$cats = array(
new Cat(array('name' => 'Kitty')),
new Cat(array('name' => 'Lily')),
);
$user->cats()->save($cats);
When specifying a relationship on User Model the Cat model also needs to exist.
In migration
Users
$table->foreign('cats_id')->references('id')->on('cats');
Cats
$table->foreign('user_id')->references('id')->on('users');
Now you force integrity on database level.
Migrate
run the migration using php artisan migrate
Next step is to add the integrity on you Model
Model
User.php
public function cats()
{
return $this->hasMany('Cats');
}
Cat.php
public function user()
{
return $this->belongsTo('User');
}