In this, I will show you an example of laravel model disable primary key. If you do not know laravel model disable auto increment then i will show you the way to enable it. I will explain to you about disable primary key in laravel model. I will simply show you create model without primary key in laravel.
You already know that by default laravel migration added the id
column as the primary key and the model added auto increment itself. But assuming you do not want to add a primary key and auto-increment key for your table. Laravel provides a way to disable the primary key and auto increment using model properties which is incrementing
.
We have to set $primaryKey as a "null" value and $incrementing as "false" to disable the primary key. See the example code:
Example code of laravel model disable primary key:
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'
];
}
Read also: Avoid Mass Assignment to Set Unguard Globally For Every Model In Laravel
Hope it can help you.
#laravel #laravel-9x