PHP Laravel 7 Inner Join Query Example (join query in laravel)

Today, We want to share with you join query in laravel.In this post we will show you PHP MySQL join Database Table, hear for laravel eloquent join 2 tables we will give you demo and example for implement.In this post, we will learn about Inner Join Query builder in laravel 7 with an example.

PHP Laravel 7 Inner Join Query Example

There are the Following The simple About laravel join two tables Full Information With Example and source code.

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

In this Article, We Shall learn all about you to use inner join query builder in laravel 7 web application project. We sh write simple inner join query using laravel 6 query builder. we would use db(), join() to write inner join query in laravel 6.

join query in laravel,inner join in laravel,join in laravel,laravel join,join laravel,laravel join query,laravel eloquent join,inner join laravel,laravel inner join,laravel join 2 tables,
PHP Laravel 7 Inner Joins Query Example

The first parameters passed to the join function is the name of the database table you required to join to, while the remaining arguments each the table column constraints for the join. You can even join to multiple Database tables in a mysql single query.

Eloquent join queries in Laravel

With the help of simple all about the joins keyword, we are used to add multiple Dataabse tables through a single mysql query. If this work is done without any joins we will have to write more than single query.
Laravel supported we three types of joins.

join query in laravel

Inner Join: – The most use used laravel 5,6 and 7join is simple INNER JOIN. When we use this join then the outputs are display in the record set which exect match in both Database tables.

$students = DB::table('students')
->join('identiticard', 'students.id', '=', 'identiticard.student_id')
->select('students.fname', 'identiticard.gender', 'identiticard.status', 'website_details.title', 'identiticard.lname', 'identiticard.address', 'identiticard.number')
->get();

Join query use with WHERE Condition

$students = DB::table('students')
->join('identiticard', 'students.id', '=', 'identiticard.student_id')
->select('students.fname', 'identiticard.gender', 'identiticard.status', 'website_details.title', 'identiticard.lname', 'identiticard.address', 'identiticard.number')
->where('identiticard.status', '=', 'active')
->get();

Join query use with Limit

$students = DB::table('students')
->join('identiticard', 'students.id', '=', 'identiticard.student_id')
->select('students.fname', 'identiticard.gender', 'identiticard.status', 'website_details.title', 'identiticard.lname', 'identiticard.address', 'identiticard.number')
->where('identiticard.status', '=', 'active')
->limit(5)
->get();

Join query use with ASC Orderby Show

$students = DB::table('students')
->join('identiticard', 'students.id', '=', 'identiticard.student_id')
->select('students.id', 'students.fname', 'identiticard.gender', 'identiticard.status', 'website_details.title', 'identiticard.lname', 'identiticard.address', 'identiticard.number')
->orderby('student.id', '=', 'asc')
->get();

Join query use with DESC Orderby Show

$students = DB::table('students')
->join('identiticard', 'students.id', '=', 'identiticard.student_id')
->select('students.id', 'students.fname', 'identiticard.gender', 'identiticard.status', 'website_details.title', 'identiticard.lname', 'identiticard.address', 'identiticard.number')
->orderby('student.id', '=', 'desc')
->get();

Join query use with paginate Query

$students = DB::table('students')
->join('identiticard', 'students.id', '=', 'identiticard.student_id')
->select('students.id', 'students.fname', 'identiticard.gender', 'identiticard.status', 'website_details.title', 'identiticard.lname', 'identiticard.address', 'identiticard.number')
->orderby('student.id', '=', 'desc')
->paginate(10);

left join in laravel

Left Join: – Left join in Laravel, if all the record set are display, then we use simple LEFT JOIN for it. By LEFT JOIN, we display that we should display all the record set of the left Database table.

DB::table('students')
->leftJoin('identiticard', 'students.id', '=', 'identiticard.student_id')
->get();

Right join in laravel

Right Join: – If we want to have all the record set display in the right Database table in Laravel or if there is no data set in the left Database table, we can use simple RIGHT simple for it.

DB::table('students')
->rightJoin('identiticard','identiticard.student_id','=','students.id')
->select('students.name','identiticard.last_name')
->get();

In this example(PHP Laravel 7 Inner Join Query), We would make two Database tables members and teams. i would display to team id to members table.

inner join query builder

public function membersGet()
{
    $members = DB::table('members')
        ->join('teams', 'teams.id', '=', 'members.team_id')
        ->select('members.*', 'teams.team_name')
        ->get()->toArray();    	

        echo '
';
        print_r($members);
        exit;
}

Inner Join Query Example : results

Array(
    [0] => stdClass Object
        (
            [id] => 1
            [team_id] => 1
            [name] => jigarbhai
            [email] => [email protected]
            [email_verified_at] => 
            [password] => 98565698
            [remember_token] => 
            [created_at] => 
            [updated_at] => 
            [team_name] => jdkpakainfo
        )

    [1] => stdClass Object
        (
            [id] => 2
            [team_id] => 2
            [name] => rajdeep
            [email] => [email protected]
            [email_verified_at] => 
            [password] => 
            [remember_token] => 
            [created_at] => 
            [updated_at] => 
            [team_name] => purple
        )
)...........
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 PHP Laravel 7 Inner Join Query 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