JIGNESH PATEL
JIGNESH PATEL

Reputation: 397

Convert sql join query to laravel

SELECT school_courses.*, schools.school_name, schools.country, schools.city  FROM 
school_courses JOIN schools ON school_courses.school_id = schools.id

Hello there,

I want to convert the above SQL query to laravel. And also I want to learn joins in laravel query please share any easy step by step tutorial link if you have one.

Upvotes: 0

Views: 59

Answers (2)

A.A Noman
A.A Noman

Reputation: 5270

My suggestion is to use better Eloquent: Relationships

Relationships

Your converted query is given below

use Illuminate\Support\Facades\DB;

$users = DB::table('school_courses')
         ->join('schools', 'us schools ers.id', '=', 'contacts.user_id')
         ->select('school_courses.*','schools.school_name', 'schools.country', 'schools.city')
         ->get();

Upvotes: 1

Prashant Deshmukh.....
Prashant Deshmukh.....

Reputation: 2292

This is Query Builder way of doing it.

   $result = DB::table('school_courses')
         ->join('schools','schools.id','=','school_courses.school_id')
         ->select('school_courses.*','schools.school_name', 'schools.country', 'schools.city')
         ->get();

Refer this link https://laravel.com/docs/8.x/queries#joins

Upvotes: 1

Related Questions