ItSolutionStuff.com

Laravel Model Disable Primary Key & Auto Increment Example

By Hardik Savani • April 16, 2024
Laravel

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...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

šŸ“ŗ Subscribe on YouTube

We Are Recommending You

ā˜…

How to Set Default Value in Laravel Model?

Read Now →
ā˜…

How to Select Specific Columns in Laravel Eloquent Model?

Read Now →
ā˜…

How to Get All Models in Laravel?

Read Now →
ā˜…

How to Get Columns Names from Model in Laravel?

Read Now →
ā˜…

How to Create Model in Laravel using Command?

Read Now →
ā˜…

Laravel 9 Enum Model Attribute Casting Example

Read Now →
ā˜…

Laravel Eloquent Model Custom Function Example

Read Now →
ā˜…

How to Create Custom Model Events in Laravel?

Read Now →
ā˜…

Laravel Replicate Model with Relationships Example

Read Now →
ā˜…

How to use Laravel Model Observers?

Read Now →
ā˜…

Laravel Improve Speed Performance using Model Caching Tutorial

Read Now →
ā˜…

How to disable model timestamps in Laravel?

Read Now →
ā˜…

How to Get Table Name from Model in Laravel?

Read Now →