Home » laravel join Multiple Tables using Model Example

laravel join Multiple Tables using Model Example

Today, We want to share with you laravel join Multiple Tables using Model.In this post we will show you laravel raw query with parameters, hear for laravel left join multiple conditions we will give you demo and example for implement.In this post, we will learn about simple select query in laravel with an example.

laravel join Multiple Tables using Model

There are the Following The simple About laravel joins 2 tables using model Full Information With Example and source code.

Read Also:  Laravel 5/6/7 Carbon date format Example

As I will cover this Post with live Working example to develop laravel subquery, so the laravel whereraw is used for this example is following below.

Inner Join Clause

$customers = DB::table('customers')
            ->join('contacts', 'customers.id', '=', 'contacts.customer_id')
            ->join('orders', 'customers.id', '=', 'orders.customer_id')
            ->select('customers.*', 'contacts.phone', 'orders.price')
            ->get();

Left Joins / Right Joins Clause

MySQL “left joins” or “right joins” instead of an “inner joins”, use the leftJoin or rightJoin methods.

$customers = DB::table('customers')
            ->leftJoin('items', 'customers.id', '=', 'items.customer_id')
            ->get();

$customers = DB::table('customers')
            ->rightJoin('items', 'customers.id', '=', 'items.customer_id')
            ->get();

Cross Joins Clause

Simple Cross joins create a cartesian product between the first table and the joined table:

Read Also:  Laravel Datatables Individual column searching using Ajax Jquery

$customers = DB::table('sizes')
            ->crossJoin('colours')
            ->get();

Advanced Joins Clauses

DB::table('customers')
        ->join('contacts', function ($join) {
            $join->on('customers.id', '=', 'contacts.customer_id')->orOn(...);
        })
        ->get();

The where and orWhere methods on a join.

DB::table('customers')
        ->join('contacts', function ($join) {
            $join->on('customers.id', '=', 'contacts.customer_id')
                 ->where('contacts.customer_id', '>', 5);
        })
        ->get();

Sub-Query Joins

joinSub, leftJoinSub, and rightJoinSub methods to joins a query to a sub-query.

$latestIteams = DB::table('items')
                   ->select('customer_id', DB::raw('MAX(created_at) as last_post_created_at'))
                   ->where('is_published', true)
                   ->groupBy('customer_id');

$customers = DB::table('customers')
        ->joinSub($latestIteams, 'latest_items', function ($join) {
            $join->on('customers.id', '=', 'latest_items.customer_id');
        })->get();

Web Programming Tutorials Example with Demo

Read :

Read Also:  PHP file_get_contents URL Examples

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel whereraw 2 table joins Example.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel Advance Database Query Builder
  2. Read Also:  Laravel 6.2 Get Last Inserted ID
  3. Read Also:  Angular 6 Form Validation with Message - Angular 6 Live Validation
  4. Read Also:  Simple VueJS date picker Component
  5. Read Also:  strstr in php
  6. Read Also:  Laravel 6 Join multiple tables Eloquent Examples
  7. Read Also:  Laravel 7 Joins clause Example Tutorial
  8. Read Also:  Woocommerce get price in custom loop
  9. Read Also:  Laravel Grouping Cache Offset & Limit Eloquent methods
  10. Read Also:  Laravel Database Eloquent Unions Query

Leave a Comment

Your email address will not be published. Required fields are marked *