File size: 850 Bytes
6966756 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?php
namespace App\Traits;
trait ApiQuery
{
public function scopeApiQuery($query)
{
$request = request();
if ($request->take) {
return $query->limit($request->take)->get();
}
if ($request->order_by) {
$query->orderBy('id', $request->order_by);
} else {
$query->orderBy('id', 'desc');
}
if ($request->calc) {
$calc = explode(':', $request->calc);
if ($calc[0] == 'sum') {
$calculation = $query->sum($calc[1]);
} else {
$calculation = $query->count();
}
return $calculation;
}
if ($request->pagination) {
return $query->paginate(getPaginate($request->pagination));
}
return $query->paginate(getPaginate());
}
} |