Laravel Model Disable Primary Key & Auto Increment Example
This post will give you an example of laravel model disable primary key. I would like to share with you laravel model disable auto increment. I’m going to show you about disable primary key in laravel model. I explained simply about create model without primary key in laravel.
By default laravel migration added id column as the primary key and the model added auto increment it. But if you don't want to add a primary key and auto-increment key for your table then how you will disable it? Laravel provides a way to disable the primary key and auto increment using model properties.
we can use this example with laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 application.
We need to set $primaryKey as a "null" value and $incrementing as "false" to disable the primary key. so, let's see the below model example code:
class City extends Model
* The primary key associated with the table.
* @var string
protected $primaryKey = null;
* Disable auto increment value
* @var string
public $incrementing = false;
* Write code on Method
* @return response()
protected $fillable = [
I hope it can help you...
We are Recommending you
- How to Set Default Value in Laravel Model?
- How to Select Specific Columns in Laravel Eloquent Model?
- How to Get All Models in Laravel?
- How to Get Columns Names from Model in Laravel?
- How to Create Model in Laravel using Command?
- Laravel 9 Enum Model Attribute Casting Example
- Laravel Eloquent Model Custom Function Example
- How to Create Custom Model Events in Laravel?
- Laravel Replicate Model with Relationships Example
- How to use Laravel Model Observers?
- Laravel Improve Speed Performance using Model Caching Tutorial
- How to disable model timestamps in Laravel?
- How to Get Table Name from Model in Laravel?