Laravel : inner join query

In this tutorial learn inner join query,  you may use the join method on a query builder instance. The first argument passed to the join method is the name of the table you need to join to, while the remaining arguments specify the column constraints for the join. You can even join to multiple tables in a single query


(in Controller AND use Illuminate\Support\Facades\DB;)


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Pandl;
use Illuminate\Support\Facades\DB;

class PandlController extends Controller
{

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        $pandl = DB::table('pandls')
                ->join('accounts', 'pandls.acc_id', '=', 'accounts.id')
                ->join('categories', 'pandls.cat_id', '=', 'categories.id')
                ->select('pandls.*', 'accounts.name', 'categories.cat_name')
                ->where('pl_type', 1)
                ->orderBy('id','DESC')
                ->paginate(10);
         return view('admin.pages.pandl',['pandl' => $pandl]);
    }

}

In View file  pandl.blade.php


@foreach($pandl as $index =>  $income)
                <tr>
                    <td>{{$index+1}}</td>
                    <td>{{$income->name}}</td>
                    <td>{{$income->cat_name}}</td>
                    <td>{{$income->amount}}</td>
                </tr>
 @endforeach

No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...