I have 2 models, Sound
and Tag
:
<?php
class Sound extends Eloquent{
protected $table = 'sounds';
protected $guarded = array('id');
public function tags()
{
return $this->belongsToMany('Tag');
}
}
<?php
class Tag extends \Eloquent {
protected $table = 'tags';
protected $guarded = array('id');
public function sounds()
{
return $this->belongsToMany('Sound');
}
}
In the controller I do lazy loading and it works as expected; the sounds
are fetched and each of them has tags
attached. The code below:
$sounds = Sound::with(array('tags'))->get();
I now want to filter the sounds
by a tag_id
(get only the sounds that have a particular tag attached) and I'm not sure if it's possible in Eloquent.
I tried eager load constraints but these would only specify the condition based on which a tag
is attached to a sound
or not, and not actually filter the sounds
.
Should I use the query builder instead?
Thank you.