Dhiraj
Dhiraj

Reputation: 2767

Laravel [QUERY] how to select contents from table 1 where condition must meet in table 2 and table 3

I am stuck in building a query where I need to select items from table 1 if the condition is met on table 2 and table 3.

I have these three tables:

company -> company_id, name, location

services -> company_id, service_id, service_name

date -> service_id, service_date //should i include company_id here?

I want to select company -> name and location if the search matches services->service_name and date->service_date.

How can I JOIN tables to obtain the desired result?

Upvotes: 1

Views: 1094

Answers (1)

user3720435
user3720435

Reputation: 1476

This isn't tested, but it will be something like the following:

$query = Company::query()
  ->select(['company.name','company.location'])
  ->leftJoin('services', 'services.company_id', '=', 'company.company_id')
  ->leftJoin('date', 'date.service_id', '=', 'services.service_id')
  ->where('services.service_name', $servicename)
  ->where('date.service_date', $servicedate);

Upvotes: 1

Related Questions