Commit da603647 by 杨树贤

添加查询过滤器

parent 0078b858
......@@ -4,6 +4,7 @@
namespace App\Http\Controllers;
use App\Http\Filters\IntegralFilter;
use App\Models\Integral;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
......@@ -17,11 +18,15 @@ class IntegralsController extends Controller
return $this->Export(0, 'ok', ['data' => $integral]);
}
public function index(Request $request)
public function index(Request $request, IntegralFilter $filter)
{
$page = $request->has('page') ? $request->page : 1;
$page_size = $request->has('page_size') ? $request->page_size : 10;
$integrals = Integral::with('integralType:id,name')
->page($request->page, $request->page_size)
->orderBy('id','desc')
->filter($filter)
->page($page, $page_size)
->orderBy('id', 'desc')
->get()->toArray();
$count = Integral::count();
......
......@@ -7,4 +7,13 @@ namespace App\Http\Filters;
class IntegralFilter extends QueryFilter
{
public function name($name)
{
return $this->builder->where('name', 'like', "%{$name}%");
}
public function exchange_status($status = 0)
{
return $this->builder->where('status', '=', $status);
}
}
\ No newline at end of file
<?php
namespace App\Http\Filters;
class UserIntegralFilter extends QueryFilter
{
public function user_id($user_id = 0)
{
return $this->builder->where('user_id', '=', $user_id);
}
public function exchange_status($exchangeStatus = 0)
{
return $this->builder->where('exchange_status', '=', $exchangeStatus);
}
}
\ No newline at end of file
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Integral extends Model
{
public $timestamps = false;
protected $hidden = [];
public function scopePage($query, $page = 1, $pageSize = 10)
{
return $query->offset(($page - 1) * $pageSize)->limit($pageSize);
}
public function integralType()
{
return $this->belongsTo(IntegralType::class, 'integral_type_id', 'id');
}
<?php
namespace App\Models;
use App\Http\Filters\QueryFilter;
use Illuminate\Database\Eloquent\Model;
class Integral extends Model
{
public $timestamps = false;
protected $hidden = [];
public function scopePage($query, $page = 1, $pageSize = 10)
{
return $query->offset(($page - 1) * $pageSize)->limit($pageSize);
}
public function scopeFilter($query, QueryFilter $filters)
{
return $filters->apply($query);
}
public function integralType()
{
return $this->belongsTo(IntegralType::class, 'integral_type_id', 'id');
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment