How to get all items with a certain property in pivot table with eloquent?
<p>I have a 'favourite' functionality for my loops table. I am trying to achieve this with a pivot table. But now I'm trying to find the most efficient way to call all the logged in users favourited loops with eloquent. </p>

<p><strong>loops table :</strong> </p>

<pre><code>    Schema::create('loops', function(Blueprint $table) {
        $table-&gt;increments('id');
        $table-&gt;string('name', 35);
        $table-&gt;string('loop_path', 255);
        $table-&gt;string('FK_user_id');
    });
</code></pre>

<p><strong>users table:</strong> </p>

<pre><code>    Schema::create('users', function(Blueprint $table) {
        $table-&gt;increments('id');
        $table-&gt;string('name');
        $table-&gt;string('password', 60);
    });
</code></pre>

<p><strong>favourites table :</strong> </p>

<pre><code>    Schema::create('favourites', function(Blueprint $table) {
        $table-&gt;increments('id');
        $table-&gt;integer('FK_user_id')-&gt;unsigned();
        $table-&gt;integer('FK_loop_id')-&gt;unsigned();
    });
</code></pre>

<p><strong>Loop.php :</strong> </p>

<pre><code>class Loop extends Model {

    protected $table = 'loops';
    public $timestamps = true;

    public function user()
    {
        return $this-&gt;belongsTo('App\User','FK_user_id','id');
    }

    public function favourites()
    {
        return $this-&gt;belongsToMany('App\User', 'favourites', 'FK_loop_id', 'FK_user_id');
    }

}
</code></pre>

<p><strong>This is how I achieve this now , but it doesn't seem efficient :</strong> </p>

<pre><code>    $loops = Loop::with('favourites')-&gt;
                   with('user')-&gt;get();

    $favouritedLoops = array();

    foreach($loops as $loop) 
    {
        //check if logged in user has favourited this
        $user_favorites = Favourite::where('FK_user_id', '=', Auth::user()-&gt;id)
            -&gt;where('FK_loop_id', '=', $loop-&gt;id)
            -&gt;first();

        if ($user_favorites != null)
        {
            array_push($favouritedLoops, $loop);
        }

    }

    return Response::json($favouritedLoops);
</code></pre>