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
- Update existing pivot table data in laravel
- Session Doesn't Work on Redirect
- Laravel get single row by id
- Update email with unique validation in laravel
- Class 'App\Rules\Hash' not found in Laravel
- Laravel order by date not working
- How to get date from created_at field in laravel
- Send OTP using textlocal api in laravel
- How to get path from current URL in Laravel
- Update if exist else insert new record in laravel
- Get the post details if it has at least one comment in comments table
- Convert input array to comma-separated string in laravel controller
- Get only 10 records from table in laravel
- How to use or operator in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- Update record after find method in lavavel
- Laravel upload file with original file name
- How to Access Array in blade laravel
- Laravel insert query not working
- How to display order by null last in laravel
- How to get data from two tables in laravel
- Sample configuration files to create laravel project with docker using wsl (window subsystem linux)
- Permanently delete a record in laravel
- Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given
- How to remove package from laravel