0
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.

1
  • If you really want to learn how to do this, then the best thing would be to attempt to write a solution in Laravel and then include that in your question. As for tutorial sites, the official documentation already does a good job of explaining joins. Commented Dec 28, 2020 at 7:21

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! for your quick response. I will refer to this link.
1

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.