Laravel 10 is a powerful PHP framework that simplifies web application development. When using Laravel’s Eloquent ORM, it automatically assumes that your table has an auto-incrementing primary key column named id
. However, in some cases, you may need to disable the auto-incrementing feature or use a different primary key column. In this article, we will explain how to disable the primary key and auto-incrementing in a Laravel model.
- Creating a Model
- Disabling Auto-Increment
- Using a Different Primary Key Column
- Using a Composite Primary Key
- Saving Records with Non-Auto-Incrementing Primary Key
Creating a Model
First, let’s create a new model in Laravel by running the following command:
php artisan make:model MyModel
Replace ModelName
with the name you want to give your model. For example, if you want to create a model for a users
table, you can run the following command:
php artisan make:model User
This will create a new User
model file in the app
directory. By default, Laravel assumes that the table name for this model is users
(i.e., the plural of the model name). If you need to specify a different table name, you can do so by setting the $table
property in the model file:
class User extends Model { protected $table = 'my_users_table'; }
In this example, the $table
property is set to 'my_users_table'
, which tells Laravel to use this table instead of the default users
table.
Keep in mind that when creating a new model, Laravel also creates a corresponding migration file in the database/migrations
directory. This migration file can be used to create the database table for the model. By default, the migration file includes a id
column with auto-incrementing enabled, but you can modify it to match your database structure.
This will generate a new model file in the app
directory.
Disabling Auto-Increment
By default, Laravel assumes that the primary key column is named id
and is auto-incrementing. However, you can disable the auto-incrementing feature by adding the following code to your model:
class MyModel extends Model { public $incrementing = false; }
Yes, setting the $incrementing
property to false
in your Laravel model will disable auto-increment for the primary key.
Here’s an example of how to do it:
use Illuminate\Database\Eloquent\Model; class MyModel extends Model { public $incrementing = false; protected $primaryKey = 'my_id_column'; }
In this example, we’ve set $incrementing
to false
to disable auto-incrementing for the primary key. We’ve also specified the name of the primary key column using the $primaryKey
property.
If you don’t specify a primary key column, Laravel assumes that the primary key column name is id
and will create a default primary key column with auto-incrementing enabled. By specifying the $primaryKey
property, you can use a custom column name for the primary key.
Note that if you disable auto-incrementing for the primary key, you’ll need to manually set a value for the primary key column when creating new records in your database.
The $incrementing
property indicates whether the IDs are auto-incrementing or not. By setting it to false
, you are telling Laravel that your primary key is not auto-incrementing.
Using a Different Primary Key Column
If you want to use a different column as the primary key, you can set the $primaryKey
property in your model:
class MyModel extends Model { protected $primaryKey = 'my_id'; public $incrementing = false; }
In this example, the my_id
column is used as the primary key, and the $incrementing
property is set to false
.
Using a Composite Primary Key
If you need to use a composite primary key (a primary key made up of multiple columns), you can set the $primaryKey
property to an array of column names:
class MyModel extends Model { protected $primaryKey = ['id', 'category_id']; public $incrementing = false; }
In this example, we’re creating a new instance of MyModel
and setting the values of the id
, category_id
, and name
columns. When we call the save()
method, Laravel will insert a new row into the database with the values of all three columns, and the combination of the id
and category_id
values will form the composite primary key for this record.
Saving Records with Non-Auto-Incrementing Primary Key
When saving records with non-auto-incrementing primary keys, you need to make sure that the primary key value is set correctly before saving the record:
$model = new MyModel; $model->my_id = 123; $model->name = 'John Doe'; $model->save();
In this example, we’re creating a new instance of MyModel
and setting the value of the my_id
column to 123
. We’re also setting the name
attribute to 'John Doe'
.
When we call the save()
method on the model instance, Laravel will insert a new row into the database with the values of the my_id
and name
columns. Since we’ve disabled auto-incrementing for the primary key, Laravel will use the value of the my_id
column as the primary key value for this record.
Conclusion:
In this article, we have explained how to disable primary key and auto-incrementing in Laravel model. By setting the $incrementing
property to false
and/or the $primaryKey
property to a different value, you can customize your model to fit your database structure. Keep in mind that when using non-auto-incrementing primary keys, you need to set the primary key value manually before saving the record.
This Article Ideas has been taken from the following websites; which are following:
https://stackoverflow.com/questions/45351425/how-to-disable-laravel-eloquent-auto-increment
How to Disable Primary Key & Auto Increment in Laravel?
https://www.nicesnippets.com/blog/how-to-model-disable-primary-key-auto-increment-tutorial
How to Disable Primary Key & Auto Increment in Laravel Model
https://laracasts.com/discuss/channels/laravel/disable-auto-increment-on-migration
Leave a Reply