Laravel How To Disable Primary Key Auto Increment in Model

Share this Article
Reading Time: 3 minutes
457 Views

Inside this article we will see the concept i.e Laravel How To Disable Primary Key Auto Increment in Model. Article contains the classified information about How to disable Laravel eloquent Auto Increment & Primary Key.

Sometimes when you work, you don’t need any primary key or auto increment column attribute for the table. So, this tutorial will be helpful for you for this. Tutorial is super easy to understand and implement it in your code as well.

This article is not for any laravel version specific. You can use this concept with Laravel v6, Laravel v7, Laravel v8 also.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Create Database & Connect

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE laravel_app;

To connect database with application, Open .env file from application root. Search for DB_ and update your details.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=root

Eloquent Model Settings

By default Laravel model have all properties enabled and ready to use like primary key, auto increment, timestamps, etc.

Also Laravel migration adds id column as the primary key and the model adds auto increment to 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.

Disable Primary Key

protected $primaryKey = null;

Disable Auto Increment Key

public $incrementing = false;

Disable Timestamps Key

public $timestamps = false;

Here, is the complete code of a model file.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFactory;

    protected $primaryKey = null;

    public $incrementing = false;
  
    public $timestamps = false;

    protected $fillable = [
        'name', 'budget'
    ];
}

We hope this article helped you to learn Laravel How To Disable Primary Key Auto Increment in Model Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Was this post helpful?

Get PHP Web Development Courses on Offer in just ₹299
Get PHP Web Development Courses on Offer in just ₹299