Döngüler tablosum için 'favori' bir işleve sahibim. Bunu bir pivot tablo ile elde etmeye çalışıyorum. Ama şimdi ben tüm kullanıcılara çağrıştıran döngülere elverişli çağrıları aramak için en etkili yolu bulmaya çalışıyorum.Özetle pivot tabloda belirli bir özellikteki tüm öğeler nasıl alınır?
döngüler tablosu:
Schema::create('loops', function(Blueprint $table) {
$table->increments('id');
$table->string('name', 35);
$table->string('loop_path', 255);
$table->string('FK_user_id');
});
kullanıcıları tablosu:
Schema::create('users', function(Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('password', 60);
});
sık tablosu:
Schema::create('favourites', function(Blueprint $table) {
$table->increments('id');
$table->integer('FK_user_id')->unsigned();
$table->integer('FK_loop_id')->unsigned();
});
Loop.php:
$loops = Loop::with('favourites')->
with('user')->get();
$favouritedLoops = array();
foreach($loops as $loop)
{
//check if logged in user has favourited this
$user_favorites = Favourite::where('FK_user_id', '=', Auth::user()->id)
->where('FK_loop_id', '=', $loop->id)
->first();
if ($user_favorites != null)
{
array_push($favouritedLoops, $loop);
}
}
return Response::json($favouritedLoops);
böyle demek istiyorsunuz? –