按传单订购喜欢Laravel 5.2

时间:2016-02-08 19:29:07

标签: javascript php mysql laravel laravel-5.2

我想根据旅游传单的数量来订购旅行传单。我已经有传单,喜欢桌子。我只是不知道如何计算传单,并在点击标签时最高顺序排序。正如你所看到的,我已经有了一个类似的计数器,每个传单都带有竖起大拇指的图标 enter image description here

传单表:

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_id'是传单ID,' likeable_type'来自于类似的模型,例如,如果一个人喜欢传单,它将是App \ Flyer,或者,如果一个人喜欢图像,那将是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();
});

这是OrderByController.php

如您所见,我已经按日期订购了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 TO COUNT THE FLYERS HERE NEED HELP HERE
         // MAYBE SOMETHING LIKE THIS, (that does not work though)

        $flyer = Flyer::findOrFail($id);

        $flyers = Like::orderBy('likeable_id')->where('likeable_id', '=',     $flyer);
        return view('travelflyers.index', ['flyers' => $flyers]);
    }


}

路线:

    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',
    ]);

刀片:

                 <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>

/ ****编辑******** /

public function flyerLikes(Flyer $flyer) {

        $flyer = Flyer::has('likeable', '>=', 1)
            ->select(DB::raw('id, count(likeable_id) as likes, user_id'))
            ->groupBy('user_id')
            ->orderBy('likes', 'desc')
            ->get();


        return view('travelflyers.index', ['flyer' => $flyer]);
}

1 个答案:

答案 0 :(得分:0)

我可以犯错,但那是某种

- 已编辑 -

$flyers = Flier::has('flyers', '>=', 1)
        ->select(\DB::raw('flyers.id, count(flyers.id) as likes, users.id'))
        ->join('likeable', 'users.id', '=', 'likeable.user_id')
        ->join('flyers', 'likeable.flier_id', '=', 'flyers.id')
        ->groupBy('users.id')
        ->orderBy('likes', 'desc')
        ->get();