Ok, I hope you've got a coffee for this one.
I have the following tables.
Workshops
- id
- title
- company_id
Locations
- id
- name
Workshop_Locations
- workshop_id
- location_id
Company
- id
- name
People
- id
- company_id
- name
- verified
I am trying to if possible create a result from the above tables but out only the verified workshops. They would be verified by the peoples table as these are linked to the companies table which in turn is linked to the workshops table.
Workshops are verified by having people that are certified assigned to them.
If I understand your question correctly:
$verifiedWorkshops = Workshop::select('workshops.*')
->join('companies', 'workshops.company_id', '=', 'companies.id')
->join('people', 'people.company_id', '=', 'companies.id')
->where('people.verified', '=', true)
->get()
A more "Laraverish" way to achieve this is using a "has many through" relationship between workshops and people.The models then would be
class Workshops extends Model
{
public function company()
{
return $this->belongsTo('App\Company');
}
public function locations()
{
return $this->belongsToMany('App\Location');
}
public function people()
{
return $this->hasManyThrough('App\People', 'App\Company');
}
}
class Company extends Model
{
public function workshops()
{
return $this->hasMany('App\People');
}
}
class People extends Model
{
public function company()
{
return $this->belongsTo('App\Company');
}
}
class Location extends Model
{
public function workshops()
{
return $this->belongsToMany('App\Workshop');
}
}
Then you could fetch the verified workshops with the following query
$workshops= Workshops::whereHas('people', function ($query) {
$query->where('verified', '=', true);
})->get();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.