Laravel Left join get last record of right table in MYSQL

Laravel Left join get last record of right table in MYSQL

In this Post We Will Explain About is Laravel Left join get last record of right table in MYSQL With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to How to select last row for each group in Laravel Example

In this post we will show you Best way to implement Left join with last record of right table in Laravel, hear for Find the last record based on a attribute of a table in Laravel with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Left join with last record of right table in Laravel & Mysql

Here Simple Laravel Query, we have 2 tables ‘students’ and ‘teachers’ where students is left table and teachers is right table which has user teachers.

We wanted to left join students with teachers but the join should be with the latest record or teachers table.

	$query = Users::select('students.id', 'students.user_name','teachers.created_at as last_activity_date')
	->leftJoin('teachers', function($query) {
	$query->on('students.id','=','teachers.stud_id')
	->whereRaw('teachers.id IN (select MAX(a2.id) from teachers as a2 join students as u2 on u2.id = a2.stud_id group by u2.id)');
	})
	->where('students.role_type_id', Users::STUDENT_ROLE_TYPE)->get();

php – Laravel leftJoin only last record of right table

$result = DB::table('posts')
->leftJoin('category',function($join)
                    {
                      $join->on('posts.id_post', '=',  'category.id_post')
                    })->whereRaw(MY_SEARCH_FILTERS)
->orderBy('category.id_cat','desc')
->first();

The solution : Left join with last record of right table in Laravel & Mysql

We can also use other example php – MYSQL: select latest record only (on left join table) (Laravel 5.1) :

$result = DB::table('posts')
          ->leftJoin('posts.id','=','category.id_post')
          ->whereRaw(MY_SEARCH_FILTERS)
          ->orderBy('category.id_cat','desc')
          ->first();

You are Most welcome in my you tube Channel Please subscribe my channel. and give me Feed Back.
More Details……
Angular Example

Example

I hope you have Got What is How to get last record of database table in La-ravel And how it works.I would Like to have Feed Back From My Blog(Pakainfo.com) readers.Your Valuable Feed Back,Any Question,or any Comments about This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Comment