
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
- Convert input array to comma-separated string in laravel controller
- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel8.projects' doesn't exist
- How to get last year records count with month wise in Laravel
- How to create projects method with belongstomany relationship in user model
- Split an Eloquent Collection by half in Laravel
- Credit card validation in laravel
- Insert Comma Separated Values in laravel
- Route prefix with auth middleware in laravel
- How to pass data to route in laravel?
- Create project factory and seed data in laravel
- Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
- Laravel get count with where condition
- How to check data inserted or deleted in pivot after toggle method
- Recursive function example code PHP Laravel
- Retain selected value of select box in Laravel
- Redirect to another view from controller in laravel
- Call to a member function pluck() on null
- How to update record after save method in Laravel
- There are no commands defined in the "route:" namespace
- Get count of filter data, while return a small set of records
- How to use or operator in laravel
- How to get column names from table in Laravel
- How to display a specific word from a string in laravel
- How to upload image in laravel 8
- Session Doesn't Work on Redirect