Thursday, February 6, 2020

How to Connect Different Table in Laravel 5.7

How to Connect  Different  Table in Laravel 5.7

This is another tutorial on the Laravel 5.7 framework. In this publication you will learn how to link multiple tables in the Laravel 5.7 framework. Here is an example of how to get data from two or more tables using the internal union in Laravel 5.7 and how to display it on the website. In Laravel 5.7, if you want to learn how to create an internal union with multiple tables, this tutorial provides a simple example that allows you to create data from multiple tables from scratch.

Linking tables means that the records or rows that are present in both tables are returned. There is at least one table column match between two tables. So, we have to do this kind of union in the Laravel 5.7 framework. The main advantage of merging multiple tables is that when you run a query, you can retrieve data from multiple tables. The following is the source for Laravel 5.7, which is used to link multiple tables using the internal union.


JoinTableController.php


app/Http/Controllers/>JoinTableController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

class JoinTableController extends Controller
{
    function index()
    {
     $data = DB::table('city')
       ->join('state', 'state.state_id', '=', 'city.state_id')
       ->join('country', 'country.country_id', '=', 'state.country_id')
       ->select('country.country_name', 'state.state_name', 'city.city_name')
       ->get();
     return view('join_table', compact('data'));
    }
}
?>


join_table.blade.php


resources/views/join_table.blade.php

<html>
 <head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>How to Connect  Different  Table in Laravel 5.7</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
 </head>
 <body>
  <div class="container">    
     <br />
     <h3 align="center">How to Connect  Different  Table in Laravel 5.8</h3>
     <br />
   <div class="table-responsive">
    <table class="table table-bordered table-striped">
           <thead>
            <tr>
                <th>Country</th>
                <th>State</th>
                <th>City</th>
            </tr>
           </thead>
           <tbody>
           @foreach($data as $row)
            <tr>
             <td>{{ $row->country_name }}</td>
             <td>{{ $row->state_name }}</td>
             <td>{{ $row->city_name }}</td>
            </tr>
           @endforeach
           </tbody>
       </table>
   </div>
  </div>
 </body>
</html>


web.php


routes/web.php

Route::get('join-table', 'JoinTableController@index');


0 comments:

Post a Comment

Please don't enter any spam link in the comment box.