javascript - Order By Flyer Likes Laravel 5.2 -
i want order travel flyers how many likes travel flyers has. have flyers , likes table. dont know how count flyers , order them highest when likwe tag clicked on. u can see, have counter each flyer thumbs icon 
flyers table:
schema::create('flyers', function (blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned(); $table->string('title'); $table->string('excerpt'); $table->text('description'); $table->timestamps(); }); likeable table:
( 'likeable_id' flyers id, , 'likeable_type' model placed, example if person liked flyer app\flyer, or if person liked image app\image )
schema::create('likeable', function (blueprint $table) { $table->increments('id'); $table->integer('user_id'); $table->integer('likeable_id'); $table->string('likeable_type'); $table->timestamps(); }); this orderbycontroller.php
as can see have order date asc, , desc
class orderbycontroller extends travelflyerscontroller { /** * @param flyer $flyer * @return \illuminate\contracts\view\factory|\illuminate\view\view */ public function traveldateasc(flyer $flyer) { $flyer = flyer::orderby('created_at', 'asc')->paginate(15); return view('travelflyers.index', ['flyer' => $flyer]); } /** * @param flyer $flyer * @return \illuminate\contracts\view\factory|\illuminate\view\view */ public function traveldatedesc(flyer $flyer) { $flyer = flyer::orderby('created_at', 'desc')->paginate(15); return view('travelflyers.index', ['flyer' => $flyer]); } public function flyerlikes(like $like) { // dont know how count flyers here need here // maybe this, (that not work though) $flyer = flyer::findorfail($id); $flyers = like::orderby('likeable_id')->where('likeable_id', '=', $flyer); return view('travelflyers.index', ['flyers' => $flyers]); } } the routes:
route::get('travelflyers/date/asc', [ 'uses' => '\app\http\controllers\orderbycontroller@traveldateasc', 'as' => 'travelflyers.asc', ]); route::get('travelflyers/date/desc', [ 'uses' => '\app\http\controllers\orderbycontroller@traveldatedesc', 'as' => 'travelflyers.desc', ]); route::get('travelflyers/likes', [ 'uses' => '\app\http\controllers\orderbycontroller@flyerlikes', 'as' => 'travelflyers.likes', ]); and blade:
<a href="{{ route('travelflyers.desc') }}" class="ui green large label"> newest </a> <a href="{{ route('travelflyers.asc') }}" class="ui orange large label"> oldest </a> <a href="{{ route('travelflyers.likes') }}" class="ui orange large label"> likes </a> /**** edit ********/
public function flyerlikes(flyer $flyer) { $flyer = flyer::has('likeable', '>=', 1) ->select(db::raw('id, count(likeable_id) likes, user_id')) ->groupby('user_id') ->orderby('likes', 'desc') ->get(); return view('travelflyers.index', ['flyer' => $flyer]); }
i can make mistake, that's kind of
-- edited --
$flyers = flier::has('flyers', '>=', 1) ->select(\db::raw('flyers.id, count(flyers.id) likes, users.id')) ->join('likeable', 'users.id', '=', 'likeable.user_id') ->join('flyers', 'likeable.flier_id', '=', 'flyers.id') ->groupby('users.id') ->orderby('likes', 'desc') ->get();
Comments
Post a Comment