Retrieving specific data from a database is a vital process in developing dynamic apps as a Laravel developer.
In this article, we will guide you through the process of querying for a single row of data in Laravel 10. Understanding how to write speedy and exact queries is vital whether you need to get a user’s profile, product details, or other specific information from your database.
In Laravel 10, there are several methods available to get single row data. Options are – Using Models, Using raw queries, etc.
Read More: Form Validation with Ajax Request in Laravel 10 Tutorial
Use the following laravel eloquent methods to get or fetch single or specific row data from database in laravel:
- Using Laravel find()
- Using Laravel firstWhere()
- Using laravel where() and first()
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.
Concept #1: Using find()
Here, we will use find() method of model. Using find() method we can get single row data set into an object format.
/** * Write code on Method * * @return response() */ public function getSingleData() { $countryID = 1; $countryData = Country::find($countryID); echo "<pre>"; print_r($countryData); }
Concept #2: Using firstWhere()
Here, we will use firstWhere() method of model.
Read More: How To Get File Size From File Path in Laravel 10 Tutorial
When we have some condition on the behalf of that for selecting data we can use firstWhere() method.
/** * Write code on Method * * @return response() */ public function getSingleData() { $countryID = 1; $countryData = Country::firstWhere("id", $countryID); echo "<pre>"; print_r($countryData); }
Concept #3: Using first()
Here, we will use first() method of model. Using first() method we can select first row data from result set.
/** * Write code on Method * * @return response() */ public function getSingleData() { $countryData = Country::first(); echo "<pre>"; print_r($countryData); }
Concept #4: Using where() and first()
Using first() & where() methods.
Read More: How To Detect Device is Mobile or Desktop in Laravel 10
We can get single row data on the behalf of some condition using where(). first() method selects first data row.
/** * Write code on Method * * @return response() */ public function getSingleData() { $countryID = 1; $countryData = Country::where("id", $countryID)->first(); echo "<pre>"; print_r($countryData); }
We hope this article helped you to learn How To Query To Get Single Row Data in Laravel 10 Tutorial in a very detailed way.
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.