I want to make my post order by user's choice.
- Post Order by Like Rate.
- 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.
答案 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();