I have 2 tables created like so :
Schema::create('educations', function(Blueprint $table){
$table->increments('id');
$table->string('title');
$table->date('from_date');
$table->date('to_date');
$table->string('summary');
$table->integer('cv_id')->unsigned();
$table->foreign('cv_id')->references('id')
->on('cvs')
->onDelete('cascade')
->onUpdate('cascade');
$table->timestamps();
});
And they are linked to another table :
Schema::create('cvs', function(Blueprint $table){
$table->increments('id');
$table->integer('student_id')->unsigned();
$table->foreign('student_id')->references('id')
->on('students')
->onDelete('cascade')
->onUpdate('cascade');
$table->string('summary');
$table->timestamps();
});
The problem is, whenever i call this line :
$student->cv->delete();
It will drop the cv but not the other attributes in tables related to it.
What am i missing ?
That's not how it works. cascade means that when you'll detele some CV from cvs table, all rows with same cv_id will be deleted from educations table. You can't expect the whole table will be deleted.
Related
I'm creating a table where I want to use the ID from this table "menu_items" to be added in another table
Table one: menu_items
Schema::create('menu_items', function (Blueprint $table) {
$table->id('menu_level_item_id');
$table->timestamps();
$table->string('menu_item_name');
$table->string('menu_item_desc');
Table 2 products
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('product_name');
$table->string('product_desc');
$table->integer('price');
$table->integer('menu_level_item_id');
$table->timestamps();
});
My aim of doing this is that I can then create a relationship between the 2 tables as they have the same key?
Is there a different approach I can take? Should I create a unique key when creating a menu item and then add this to the second table?
Thank you.
Basically Laravel Eloquent do the key handling. When you have two tables which both has as key the name id, this is not a problem. Name the keys in the relation table just like this
table1_id
table2_id
Laravel will handle this in Eloquent. You are also able to name the two columns in the relation table to what ever you want. You could define it for the relation in Eloquent. E.g.
public function otherModel () {
return $this->belongsToMany('App\Models\OtherModel', 'table_name', 'this_model_id', 'other_model_id');
}
Please have a look into:
Laravel Relationship Documentation
Schema::create('menu_items', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('menu_item_name');
$table->string('menu_item_desc');
table->timestamp();
});
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('product_name');
$table->string('product_desc');
$table->integer('price');
$table->unsignedBigInteger('menu_level_item_id');
$table->timestamps();
$table->foreign('menu_level_item_id')->references('id')->on('menu_items');
});
Basically, I have 2 tables in my database: Games and Teams.
Every Game must have 2 teams, so it's a two-to-many relationship.
Should I use 2 foreign keys in my Games table pointing to the 2 teams in the Teams table and have a one-to-many relationship, or use many-to-many relationship with a third table to link the games and teams table?
Im using Laravel 6.5 for the project, so I guess im using Eloquent to implement it.
Schema::create('games', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('team_a_id');
$table->foreign('team_a_id')->references('id')->on('teams')->onDelete('restrict');
$table->unsignedBigInteger('team_b_id');
$table->foreign('team_b_id')->references('id')->on('teams')->onDelete('restrict');
$table->unsignedInteger('team_a_score');
$table->unsignedInteger('team_b_score');
$table->string('status');
$table->boolean('finished');
});
Schema::create('teams', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('abbreviation');
$table->string('country');
$table->timestamps();
});
This are the two tables I have created by now, is this the correct way to implement it?
Use a Many to Many Relationship. In that case the Game Modal could have multiple Teams and vice-versa.
Hence the migrations would be like:
Schema::create('games', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('status');
$table->boolean('finished');
});
Schema::create('teams', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('abbreviation');
$table->string('country');
$table->timestamps();
});
Schema::create('game_team', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('game_id');
$table->integer('team_id');
$table->unsignedInteger('score');
$table->timestamps();
});
laravel is there a way to make, one to one relationship, one table belongs to two different tables one to one relationship.
i'm trying to model, a laravel based project, that is voting for ministers.
these are my three models.
members migration. ex: members is working as users for authentication
public function up()
{
Schema::create('members', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('username');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
2.ministers migration file
public function up()
{
Schema::create('ministers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->mediumText('description');
$table->string('image')->default('no-image.jpg');
$table->unsignedInteger('votes')->default('0');
$table->boolean('status')->default('0');
$table->timestamps();
});
}
3.voters migration file
public function up()
{
Schema::create('voters', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('phone');
$table->enum('education', ['primary', 'intermediate', 'high-school', 'university' ] )->nullable(false)->default('primary');
$table->string('country');
$table->unsignedBigInteger('minister_id');
$table->foreign('minister_id')->references('id')->on('ministers');
$table->unsignedBigInteger('member_id');
$table->foreign('member_id')->references('id')->on('members');
$table->timestamps();
});
}
this voters migration belongs to member and minister, and mister associates one voter, user associates to one voter.
so is this correct way to model, a system like this. which i want every registered member, fill out voting form, which selects a drop down field of the minister to be voted for. and save that form creating a new instance of voter. What i want also is two increment by one minister votes field when ever a new instance of voter is created.
I have a migration with multiple tables in it: Status, Status_project, Status_task. Can i call them all with just my Status model that was created with migration command ? php artisan make:model Status -m
I am reading at several places that i have to make a model for each table, but is there no other way? Except DB::table('statuses'). Because of my relations i don't prefer multiple models
Example:
Schema::create('statuses', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->unique();
$table->string('display_name')->nullable();
$table->string('description')->nullable();
$table->timestamps();
});
Schema::create('status_task', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('user_id')->nullable();
$table->unsignedBigInteger('task_id');
$table->unsignedBigInteger('status_id');
$table->foreign('user_id')->references('id')->on('users')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('task_id')->references('id')->on('ongoing_tasks')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('status_id')->references('id')->on('statuses')
->onUpdate('cascade')->onDelete('cascade');
});
Schema::create('status_project', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('user_id')->nullable();
$table->string('project_id');
$table->unsignedBigInteger('status_id');
$table->foreign('user_id')->references('id')->on('users')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('project_id')->references('id')->on('ongoing_projects')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('status_id')->references('id')->on('statuses')
->onUpdate('cascade')->onDelete('cascade');
});
DB::commit();
If you plan on using Eloquent to build your queries, you need to create a model for each table. If you are using the query builder, no need to create a model for each table.
The many to many relationship works. The original working migration:
Schema::table('tag_topic', function (Blueprint $table) {
$table->integer('topic_id')->unsigned()->index();
$table->integer('tag_id')->unsigned()->index();
});
I want the pivot table items to be deleted when referenced items are deleted. The new migration:
Schema::table('tag_topic', function (Blueprint $table) {
$table->foreign('topic_id')->references('id')->on('topics') ->onDelete('cascade');
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
});
The pivot table row items are not cascaded deleted.
Update:
I dropped the table and rewrote the the migration, but still the pivot rows are not cascade deleted:
Schema::create('tag_topic', function (Blueprint $table) {
$table->integer('topic_id')->unsigned();
$table->integer('tag_id')->unsigned();
$table->foreign('topic_id')->references('id')->on('topics')->onDelete('cascade');
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
});
This code deletes the pivot table rows:
$tag = Tag::where('id', $tag->id)->get()->first();
$Tag->topics()->detach();
Try dropping your old indexes before applying the foreign key constraints.
Schema::table('tag_topic', function (Blueprint $table) {
$table->dropIndex(['topic_id']);
$table->dropIndex(['tag_id']);
$table->foreign('topic_id')->references('id')->on('topics')->onDelete('cascade');
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
});