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.

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:

$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 :

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.

Leave a Comment