How to Join Multiple Tables in Laravel 8 using Eloquent Model

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends in this video tutorial we are going to learn how to join multiple table in laravel 8 by using eloquent model for fetching data from multiple table in my sql database that means here we will fetch data from multiple table using eloquent join and display on web page under this laravel 8 framework application in this tutorial we will describe you step by step how to fetch data from multiple by join multiple using laravel eloquent model here we have used laravel eloquent join because it has give use better query performance than simple laravel join query now let's start discussing this topic so here we have testing database in which we have three table like country state and city in all table we have already inserted some data in city table city id primary key and state id is foreign key for join table same way in state table state id is primary key and country id is foreign so we will join this three table using laravel eloquent model and display all country name state name and city name data from this three table on web page in html table format now first we want to download latest version of laravel framework so we have go to command prompt and here we have go into directory in which we want to download laravel 8 framework after this here we have run this command this command will download latest version of laravel framework it will take some time for download because in latest version of laravel framework it has download more than 100 class with download framework now here we can see that laravel framework has been successfully downloaded now we have go to text editor and under that we have opened this downloaded laravel framework so this is our laravel application folder so here first we want to make database connection so here we have opened this dot environment file and under this file we have defined my sql database configuration like host name set to localhost database name set to testing user name set to root and password set with blank value so after define this details it will make database connection next we want to make model class for database operation so we have go to command prompt and here we have run this command this command will make country model under models directory so now we have go application folder and under this we have go to models directory and here we have open country.php model class file under this class first we want to define table name so here we have write protected with dollar table variable is equal to country so here we have defined country table name after this we want to define table column name so here we have write protected with dollar fillable variable is equal to array and under this array we have write country name so here our country model class is ready next we want to make one controller so we have go to command prompt and write this command so after run this command it will make join table controller under controllers folder so again we have go to application folder and under this we have open http folder and then after we have go into controllers folder and here we can see join table controller class file so we have opened this file under this file first we want to import country model class file under this class so here we have write use statement with application folder name slash models folder name slash country model class so this way we can define model class under this controller class after this under this class we have make index method this is root method of this controller class under this method we have write dollar data variable is equal to country class with join method for argument so in first argument we have write state table name in second argument we have write state dot country id in third argument we have write equal sign and in last argument we have write country dot country id so this code will join country table with state table after this we want to join city table also so here we have add join method with four argument so here in first argument we have write city table in second argument we have write city dot state id in third argument we have write equal sign and in last argument we have write state dot state id so this code will make join query for join city table with state table next we want to define which table column data we want to fetch from this three table so here we have write get method and under this method we have write array and under this we have write country dot country name table column state table dot state name table column and city table dot city name table column so this three column data will be fetch with this select query so here our join three table code is ready and this code will produce select query with join three table using laravel eloquent model for fetch data from multiple table in single query now here we have write return statement with view method with two argument in first argument we have right join table and in second argument we have write compact method and under this we have write data so view page we can fetch data from this data variable now we have go to jointable.blade.php file and here we have first create table with three table column like country state and city for fill data under this table so for this here we have right at the rate for each expression and under condition we have write dollar data as dollar row variable by using this loop it will fetch data from dollar data variable first we want to close this loop so for this here we have right at the right end for each loop between this loop we have first right open and close table row with three blank table data tag in first table column we want to print country name so here we have write expression and between this we have write dollar row variable with country name table column in second column we want to print state name so here we have write expression and between this we have write dollar row variable with state name table column in third column we want to print city name so here we have write laravel expression and between this we have write dollar row variable with city name it will fill this table data get from multiple table using laravel eloquent model lastly we want to make route for index method of join table controller so we have go to routes folder and under this we have open web dot php file under this file we want to define join table controller at header of this file so here we have write use statement with application folder name slash http folder name slash controllers folder slash join table controller after this for set root here we have write route class with get method with two argument in first argument we have right join table and in second argument we have right array and under this we have right join table controller class and in second index we have write index so when in browser we have hit laravel application base url slash join table then it will execute index method of this join table controller now for check output in browser we have to start laravel application so in command prompt we have run php artisan serv command so this command will start laravel application server and provide use base url of our laravel application friends here we can see in url we have right base url slash join table then it has load webpage in browser here we can see data country name state name and city name data has been load from country table state table and city table here we have fetch data from three table in single query by using laravel eloquent join model here we have use eloquent model because to create better relationship and get the result in view with so much simple syntax then we need eloquent model in join multiple table so this way we can join multiple table in laravel 8 framework by using eloquent model and display multiple table data on web page in html format lastly if you have any query or inputs regarding this tutorial you can ask your query in comment box if you have liked this video please share with your friends or even you can also share on social media also and lastly do not forget to subscribe our channel for future
Info
Channel: Webslesson
Views: 46,185
Rating: undefined out of 5
Keywords: laravel 8, join, laravel joins tutorial, inner, joins in laravel, fetch data from two tables in laravel, condition, laravel 8 tutorial, eloquent, joins in laravel 8, get data from multiple table, multiple, laravel multiple table join, using, model, mysql, inner join condition in laravel, inner join in laravel eloquent, laravel eloquent join tables, laravel inner join multiple on, laravel inner join example, multiple table join in laravel, laravel table join
Id: IcZ6HteoI3A
Channel Id: undefined
Length: 12min 25sec (745 seconds)
Published: Mon Apr 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.