
How to set column as primary key in Laravel model
How to set column as primary key in Laravel model
In this session, we are going to try to solve the "How to set column as primary key in Laravel model" puzzle by using the computer language.
You can set a custom column as primary key in Laravel model. You have to pass the column name to protected $primaryKey property in model class. Now your custom key is treated as primary key of table.-
Set a column as primary key in Laravel model
protected $primaryKey = 'userid';
After implementation your code will look like this
App\Models\User.php
<?php namespace App\Models; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Support\Facades\Event; class User extends Authenticatable { protected $primaryKey = 'userid';
By default, Eloquent models expect for the primary key to be named 'id'. If that is not your case, you can change the name of your primary key by specifying the $primaryKey property. Now your custom key (userid) will be treated as primary key in laravel app.
It helps you to get the records using eloquent with find() method by passing the value of userid column value. Eg: $user = App\Models\User::find(1);
Note: The data type of your custom column (userid) for primary key should be integer.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to get all route list
- Laravel upload file with original file name
- Get content from web URL in laravel
- How to change default timestamp fields name in Laravel
- Remove public from url in laravel project
- Trying to get property 'title' of non-object
- How to check duplicate entry in laravel
- Composer create project laravel/laravel example app
- Target class [admin] does not exist.
- How to get single column value in laravel
- Insert current date time in a column using Laravel
- How to pass two variables in HREF in laravel
- How to Access Array in blade laravel
- Get comma separated email from input array
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- How to insert multiple rows in mysql using loop in laravel?
- Multiple Level eager loading in Laravel
- Laravel get count with where condition
- Link storage folder in laravel 8
- How to pass link from controller to view in laravel on ajax call
- How to validate URL with https using regex in laravel
- How to get specific columns using Laravel eloquent methods
- How to disable timestamps in laravel
- How to create belongstomany relation using custom name on custom pivot table
- How to display 1 day ago in comments in laravel view