I want to make my attribute as an autoIncriment with the Id
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->decimal('price', 17, 6)->nullable();
$table->string('photo');
$table->decimal('reference')->autoIncrement()->unique()->from(1000)->unsigned();
$table->boolean('active');
$table->timestamps();
});
}
i have created this function and i want to make the reference auto increment start from 1000 , when i migrate it nothing change in my database
Try setting up the reference field to an unsignedInteger first.
Example:
$table->unsignedInteger('reference')->->autoIncrement()->unique()->from(1000);
This error popup:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`ltfrbr10infosystem`.`franchises`, CONSTRAINT `franchises_operator_id_foreign` FOREIGN KEY (`operator_id`) REFERENCES `operators` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
Laravel Migrration:
public function up()
{
Schema::create('franchises', function (Blueprint $table) {
$table->increments('id');
$table->integer('operator_id')->nullable()->unsigned();
$table->foreign('operator_id')->references('id')->on('operators')->onDelete('cascade')->onUpdate('cascade');
$table->string('case_number')->nullable();
$table->string('business_address')->nullable();
$table->date('date_granted')->nullable();
$table->date('expiry_date')->nullable();
$table->string('route_name')->nullable();
$table->string('deno')->nullable();
$table->integer('authorize_units')->nullable();
$table->string('remarks')->nullable();
$table->timestamps();
});
}
I have tried this but still it gives me error
$table->integer('operator_id')->nullable()->unsigned()->change();
I also tried this
$table->integer('operator_id')->unsigned()->default(null);
How do I make operator_id foreign key default to null?
If the data on your database is not important you could refresh your migrations and your database using
php artisan migrate:refresh
This will rollback and migrate all your migrations again. Make sure you wrote the down method right,
also, you migration should look like this:
public function up()
{
Schema::create('franchises', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('operator_id')->nullable();
$table->foreign('operator_id')->references('id')->on('operators')->onDelete('cascade')->onUpdate('cascade');
$table->string('case_number')->nullable();
$table->string('business_address')->nullable();
$table->date('date_granted')->nullable();
$table->date('expiry_date')->nullable();
$table->string('route_name')->nullable();
$table->string('deno')->nullable();
$table->integer('authorize_units')->nullable();
$table->string('remarks')->nullable();
$table->timestamps();
});
}
Other way to do it is creating a new migration like this:
public function up()
{
Schema::table('franchises', function (Blueprint $table) {
$table->unsignedInteger('operator_id')->nullable()->change();
});
}
I think you get this error because the record you are trying to insert contains a wrong value for the column operator_id. This value is not a correct operator id and is not null (it could be 0, "null" or empty string, ...)
Can you paste here the exact SQL query which rises this error ?
Add this inside up function and run php artisan migrate:refresh --seed
public function up(){
Schema::disableForeignKeyConstraints();
Schema::create('franchises', function (Blueprint $table) {
$table->increments('id');
$table->integer('operator_id')->unsigned()->nullable();
$table->foreign('operator_id')->references('id')->on('operators')->onDelete('cascade')->onUpdate('cascade');
$table->string('case_number')->nullable();
$table->string('business_address')->nullable();
$table->date('date_granted')->nullable();
$table->date('expiry_date')->nullable();
$table->string('route_name')->nullable();
$table->string('deno')->nullable();
$table->integer('authorize_units')->nullable();
$table->string('remarks')->nullable();
$table->timestamps();
});Schema::enableForeignKeyConstraints();}
you should use this
$table->foreign('operator_id')->references('id')->on('operators')->nullable()->onDelete('cascade')->onUpdate('cascade');
i cant figure out what im doing wrong, but everything looks ok, is giving me this errroof
Foreign key constraint is incorrectly formed
on my migrations, but i dont see any issue.
Migration table 1:
public function up()
{
Schema::create('candidate_industries', function (Blueprint $table) {
$table->increments('id');
$table->integer('candidate_id')->unsigned();
$table->foreign('candidate_id')->references('id')->on('candidates');
$table->integer('industry_id')->unsigned();
$table->foreign('industry_id')->references('id')->on('industries');
});
}
Migration number 2:
public function up()
{
Schema::create('candidate_regions', function (Blueprint $table) {
$table->increments('id');
$table->integer('candidate_id')->unsigned();
$table->foreign('candidate_id')->references('id')->on('candidates');
$table->integer('region_id')->unsigned();
$table->foreign('region_id')->references('id')->on('regions');
});
}
The issue was because "candidates" id column had a different dataType set, in my case was BigInt when i was trying to create a relation with a column of int.
How to set a comment on table using Laravel Schema Builder?
Column set:
public function up()
{
Schema::create('vendors', function (Blueprint $table)
{
$table->comment('not working - error'); // not working - error
$table->increments('id');
$table->string('vendor', 255)->comment('Some comment.');
$table->timestamps();
});
}
But for the table?
Well I don't have a nice answer for you, but at least it works.
Here it is:
public function up()
{
$tableName = 'vendors';
Schema::create($tableName, function (Blueprint $table)
{
$table->increments('id');
$table->string('vendor', 255)->comment('Some comment.');
$table->timestamps();
});
DB::statement("ALTER TABLE `$tableName` comment 'My comment'");
}
Just add a DB statement after creating your table.
Before Laravel 9, only columns were allowed to comment. Since laravel 9 the ability to comment on the table itself has been added.See this PR and the Blog article
If you would like to add a "comment" to a database table, you may invoke the comment method on the table instance. Table comments are currently only supported by MySQL and Postgres:
Schema::create('calculations', function (Blueprint $table) {
$table->comment('Business calculations');
// ...
});
Simple question: I'm new to Laravel. I have this migration file:
Schema::create('lists', function(Blueprint $table) {
$table->increments('id');
$table->string('title', 255);
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
I want to update it to add onDelete('cascade').
What's the best way to do this?
Firstly you have to make your user_id field an index:
$table->index('user_id');
After that you can create a foreign key with an action on cascade:
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
If you want to do that with a new migration, you have to remove the index and foreign key firstly and do everything from scratch.
On down() function you have to do this and then on up() do what I've wrote above:
$table->dropForeign('lists_user_id_foreign');
$table->dropIndex('lists_user_id_index');
$table->dropColumn('user_id');
In Laravel 7 it can be done in one line
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
let's say you have two tables student and section , you can refer the following two table structure for adding foreign key and making onDelete('cascade') .
Table -1 :
public function up()
{
Schema::create('student', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('address');
$table->string('phone');
$table->string('about')->nullable();
$table->timestamps();
});
}
Table - 2:
public function up()
{
Schema::create('section', function (Blueprint $table) {
$table->id();
$table->bigInteger('student_id')->unsigned()->index()->nullable();
$table->foreign('student_id')->references('id')->on('student')->onDelete('cascade');
$table->string('section')->nullable();
$table->string('stream')->nulable();
$table->timestamps();
});
}
hope it will help you -:)
you can read the full article from here .
Schema::create('roles',function(Blueprint $table){
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
Schema::create('permissions',function(Blueprint $table){
$table->unsignedBigInteger('role_id');
$table->foreign('role_id')->references('id')->on('roles');
$table->string('permission');
});
As of Laravel 8:
$table->foreignIdFor(OtherClass::class)->constrained();
So simple :)
Make sure that the OtherClass migration file is running EARLIER (by filename date as usual).
If the OtherClass id is not autoincrementing, the otherclass_id would have a type of char instead of bigint, in which case->
Use this instead:
$table->foreignId('otherclass_id')->index()->constrained()->cascadeOnDelete();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
In this example, we are stating that the user_id column references the id column on the users table. Make sure to create the foreign key column first! The user_id column is declared unsigned because it cannot have negative value.
You may also specify options for the "on delete" and "on update" actions of the constraint:
$table->foreign('user_id')
->references('id')->on('users')
->onDelete('cascade');
To drop a foreign key, you may use the dropForeign method. A similar naming convention is used for foreign keys as is used for other indexes:
$table->dropForeign('posts_user_id_foreign');
If you are fairly new to Laravel and Eloquent, try out the Laravel From Scratch series available on laracasts. It is a great guide for beginners.
Laravel 7.x Foreign Key Constraints
Laravel also provides support for creating foreign key constraints, which are used to force referential integrity at the database level. For example, let's define a user_id column on the posts table that references the id column on a users table:
Schema::table('posts', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
Since this syntax is rather verbose, Laravel provides additional, terser methods that use convention to provide a better developer experience. The example above could be written like so:
Schema::table('posts', function (Blueprint $table) {
$table->foreignId('user_id')->constrained();
});
Source: https://laravel.com/docs/7.x/migrations
You should create a new migration file let's say 'add_user_foreign_key.php'
public function up()
{
Schema::table('lists', function(Blueprint $table)
{
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('lists', function(Blueprint $table)
{
$table->dropForeign('user_id'); //
});
}
The run
php artisan migrate
If you want to add onDelete('cascade') on the existing foreign key, just drop the indexes and create them again:
public function up()
{
Schema::table('lists', function($table)
{
$table->dropForeign('lists_user_id_foreign');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
public function down()
{
Schema::table('lists', function($table)
{
$table->dropForeign('lists_user_id_foreign');
$table->foreign('user_id')->references('id')->on('users');
});
}
Schema::table('posts', function (Blueprint $table) {
$table->unsignedInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
for versions before 7x;
Schema::create('lists', function(Blueprint $table) {
$table->increments('id');
$table->string('title', 255);
$table->unsignedBigInteger('user_id')->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
for version 7+;
Since this syntax is rather verbose, Laravel provides additional, terser methods that use conventions to provide a better developer experience. When using the foreignId method to create your column, the example above can be rewritten like so:
Schema::create('lists', function(Blueprint $table) {
$table->increments('id');
$table->string('title', 255);
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->timestamps();
});
The foreignId method is an alias for unsignedBigInteger while the constrained method will use convention to determine the table and column name being referenced. If your table name does not match the convention, you may specify the table name by passing it as an argument to the constrained method:
Schema::create('lists', function(Blueprint $table) {
$table->foreignId('user_id')->constrained('users')->onDelete('cascade');
});
source: https://laravel.com/docs/7.x/migrations#foreign-key-constraints
Clear and new in laravel
$table->foreignId('book_id')->constrained();
I was doing the same but got error " id not exist" => so I changed my migration file as below :
question table migration content:
$table->id() => should change to $table->increments('id')
definitions of foreign key in Reply table:
$table->foreign('question_id')->references('id')->on('questions')->onDelete('cascade');
now your foreign key will work.
Clear, modern and Straightforward approach
suppose parent: `Book Model` and `books table`
suppose child : `Page Model` and `pages table`
$table->foreignId('book_id')->references('id')->on('books');
where book_id is is the colomn name in child (pages table)
and id is the linkage between the Parent and Child tables, books and pages tables, and books is the table name to which we are going to link