Laravel Model Disable Primary Key & Auto Increment Example
Hi Guys,
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, laravel 10 and laravel 11 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:
app/Models/City.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class City extends Model
{
use HasFactory;
/**
* 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 = [
'name', 'state_id'
];
}
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
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?