如何从<a> tag value into Controller in Laravel 5.2

时间:2016-11-09 08:15:34

标签: php laravel

I want to make my post order by user's choice.

  1. Post Order by Like Rate.
  2. Post Order by Date.

So here is my index.blade.php code

<li class="dropdown text-right" id="orderlist" name="order"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Post Order <span class="caret"></span></a>

       <ul class="dropdown-menu">
           <li><a value="polike" id="like">By Like</a></li>
           <li><a value="podate" id="date">By Date</a></li>
       </ul>
</li>

I want to get that Value from both of < a > tag and carry that values with variable to Controller.

Here is my controller Code.

//Logic order by
        $value="value from inded.blade.php"
        $order=$value;

        if($order=="bydate"){ //Order by post date
            orderBy('created_at','desc')
        }
        else{ //Order by Like Rating.
            $infos=Funs::bubblesort($infos);
        }

Thank you for your helping. Please help me.

1 个答案:

答案 0 :(得分:2)

You can build your links so they will pass GET parameter:

<li><a href="post?order=like">By Like</a></li>
<li><a href="post?order=date">By Date</a></li>

Then in a model you can create order() local scope:

public function scopeOrder($q)
{
    if (!empty(request()->order)) { 
        $q = request()->order == 'like' ? $q->orderBy('like', 'desc') : $q;
        $q = request()->order == 'date' ? $q->orderBy('date', 'desc') : $q;
    }

    return $q;
}

And then use it:

Model::order()->get();