0

I am new to Laravel and finding it a bit hard to learn it. I am having trouble in writing an SQL query that involves joining between multiple tables. I viewed the documentations but it did not understand how to write it.

This is my raw sql query which I want to write in Laravel style:

SELECT 
cd.`company_details`,cd.`company_id`,cd.`company_logo`,cd.`company_name`,
cd.`company_type_id`,cd.`company_website`,cd.`login_email`,cd.`phone_number,                   ld.`date_created`,ld.`is_active`,ld.`login_password`,ld.`login_type`
FROM `company_details` AS cd
JOIN `login_details` AS ld
ON cd.`login_email`=ld.`login_email`
WHERE cd.`login_email`=$login_email
AND cd.`company_id`=$company_id
AND cd.`company_name`=$company_name 
AND ld.`login_type`='COMPANY'

I know the basic syntac like DB::tablename()->select()->where()->get() but can't write the query. Please Help.

1 Answer 1

2
 DB::table('company_details AS cd')
  ->join('company_details AS ld', 'ld.login_email', '=', 'cd.login_email')
  ->where('cd.login_email', $login_email)
  ->where('cd.company_id', $company_id)
  ->where('cd.company_name', $company_name)
  ->where('ld.login_type', 'COMPANY')
  ->select('cd.company_details, cd.company_id, cd.company_logo, cd.company_name, cd.company_type_id, cd.company_website, cd.login_email, cd.phone_number')
->get();

For joining multiple tables read this documentation: https://laravel.com/docs/5.4/queries#joins

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

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.