分页
使用forPage代替offset+limit(在项目里看到这个好难受,尤其是每次计算offset偏移量)
namespace Tests\Feature;
use App\Models\Auth\User;
use Tests\TestCase;
class PageTest extends TestCase
{
/**
* A basic feature test example.
*
* @return void
*/
public function test_example()
{
$page = 2;
$pageSize = 20;
$columns = ['id', 'mobile', 'name'];
$re_1 = User::query()->limit($pageSize)->offset(($page - 1) * $pageSize)->get($columns);
# 两者等价,使用forPage更优雅
$re_2 = User::query()->forPage($page, $pageSize)->get($columns);
$this->assertEquals($re_1, $re_2);
}
}
或者使用paginate(好处在于返回了一些比较有用的东西,如total,lastPage,currentPage 等,可以少写一些逻辑,只管去取值组装)
namespace App\Http\Controllers\Test;
use App\Models\Auth\User;
use Illuminate\Http\Request;
/**
* Class IndexController
* @package App\Http\Controllers\Test
*/
class IndexController
{
/**
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function index(Request $request)
{
$res = User::query()->paginate(10, ['id', 'name', 'mobile'], '', 2);
$res->total(); #总条数
$res->lastPage(); #最大页数
$res->items(); #数据
}
}
敏感信息的隐藏
通过model的hidden属性设置要隐藏的字段(对toArray有效)
老项目增加需求,说用户收到ZP电话。需要对业务数据涉及到用户电话部分不予以展示。要通过指定接口获取用户电话信息。
发现项目大部分都是使用了User模型的toArray。通过设置User的hidden解决了大部分接口返回电话号码的问题了。少部分自己手动处理。
namespace App\Models\Auth;
use App\Models\Traits\RichSoftDeletes;
class User extends \Illuminate\Database\Eloquent\Model
{
use RichSoftDeletes;
protected $fillable = ['name', 'mobile', 'password', 'creator', 'updater'];
protected $hidden = ['mobile'];
public function toArray()
{
$res = parent::toArray();
$values = array_values($res);
$keys = array_keys($res);
$keys = array_map(function ($key) {
return ucfirst($key);
}, $keys);
return array_combine($keys, $values);
}
}
设置查询返回的字段类型
设置model的casts属性来指定字段类型
比较常用的是将json转换成array。这样我就不需要手动转了
measure_info存储的是json格式
不使用casts的话。需要自己手动去json_decode。
namespace App\Models\Order;
use Illuminate\Database\Eloquent\Model;
class OrderInfo extends Model
{
protected $table = 'factory_custom_order_info';
protected $primaryKey = 'custom_info_id';
protected $casts = [
'measure_info' => 'array'
];
}
关于错误信息的处理
那个,能用异常用异常抛出。在Laravel App\Exceptions\Handler中统一处理。
我看到那个return code 的那种。在老项目里看到最多12层的那种接受返回值,整个人都不好了。
我调用你封装的方法。还要一个一个去判断你的返回值,太恶心。
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
* @var array<int, class-string<Throwable>>
*/
protected $dontReport = [
BusinessException::class,
];
/**
* A list of the inputs that are never flashed for validation exceptions.
*
* @var array<int, string>
*/
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
}
public function render($request, Throwable $e)
{
if ($e instanceof BusinessException) {
return response()->json([
'code' => $e->getCode(),
'message' => $e->getMessage(),
], 200);
}
return parent::render($request, $e); // TODO: Change the autogenerated stub
}
}