I have three tables and models related to each other and I need to consult the properties whose Environment.Name = 'Dormitorios'
and PropertyEnvironment.Value = 2
:
App\Property
:
- id
- address
- etc
Methods:
public function environments()
{
return $this->hasMany('App\PropertyEnvironment');
}
App\Environment
:
- id
- name
Methods:
public function value()
{
return $this->hasOne('App\PropertyEnvironment');
}
App\PropertyEnvironment
:
- id
- property_id
- environment_id
- value
Methods:
public function environment()
{
return $this->belongsTo('App\Environment');
}
public function property()
{
return $this->belongsTo('App\Property');
}
I think the corresponding query would be something like this:
SELECT *
FROM properties p
INNER JOIN property_environments pe ON pe.property_id = p.id
INNER JOIN environments env ON pe.environment_id = env.id
WHERE (env.name = 'Dormitorios')
AND (pe.value = 2);
Any ideas? I thank you in advance