Inside this article we will see How to get single row data in laravel 8 application. Article contains very classified information about Laravel 8 methods which helps to get single row data from database.
If you are looking for an article which makes you understand about how to get a single row from table then you are at the right place to learn that.
In Laravel 8, there are several methods available to get single row data. Also selecting data rows from database in Laravel 8, we have several options available as – Using Models, Using raw queries, etc.
Learn More –
- API Authentication using Laravel 8 Sanctum Tutorial
- Bar Chart Integration with Laravel 8 – HighCharts Js
- Barcode Generator in Laravel 8 Tutorial
- Bootstrap Growl jQuery Notification Plugin to Laravel 8
Let’s get started.
Laravel Installation
We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.
Here is the command to create a laravel project-
composer create-project --prefer-dist laravel/laravel blog
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()
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()
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()
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. 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 How To Get Single Row Data in Laravel 8 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.