How to fetch data from multiple tables in laravel

Step 1: Download Laravel Framework

For download fresh copy of Laravel framework, so first we have to into command prompt and run following command. This command will make join_table directory and under that directory it will download Laravel framework latest version.

composer create-project --prefer-dist laravel/laravel join_table "5.8.*"

Step 2: Make Database connection

After download and install Laravel framework and after this we want to make first database connection. So for make database connection, we have to open .env file and under this file, we have to define mysql database configuration. So it will create database connection in Laravel framework.

DB_DATABASE=testing
DB_USERNAME=root

Step3: Sql file import

Once you have make database connection, then after we have to make table in mysql database. So for this you have to run following sql script in your local database, so it will create table in your define mysql database.

Step3: Create Model Class

In Laravel framework, we have to create model class for database related operation. So for create model classe using compost, we have go to command prompt and run following command, this command will make model class file with name Country.php under app/Models folder.

php artisan make:model Country

After create model class, we have to open that file and under that file we have to define mysql table name details and column details.

Step4:Create Controller Class

Under this Laravel framework, for handle HTTP request we have to create controller class. So for create controller using compost, we have go to command prompt and run following command.

php artisan make:controller JointableController

Once your controller class has been create then for open that file, we have go to app/Http/Controllers/JointableController.php file and under this file you have write following code for join multiple table using eloquent model and fetch data from multiple table in single query execution.

Step4: Create View Blade file

For display HTML output in browser, so we have to create view blade file in Laravel framework. In Laravel framework view blade file has been store under resources/views folder. Under this file, we have create join_table.blade.php file. Under this file, it will received data from controller class file. You can find source code of this view blade file in below.

Step 5: Set Route

Under this Laravel framework, we have to set route of the controller method. For set route in Laravel 8 framework, we have to open routes/web.php file. In Laravel 8 framework for set route, we have to first import our controller class under this web.php file. And for set route you can find source code below.

Step 6: Run Laravel Server

After follow all above steps now all are set now we want to run Laravel application in browser. So for this, we have to go command prompt and run following command.

php artisan serve

Once you have run above command so after run this command it will provide us base url of our Laravel application. So for check above script output, we have to hit following url in browser.

http://127.0.0.1/join_table

Tagged :

Leave a Reply