laravel通用apiResource get update
2019-06-25 本文已影响0人
IT宝哥哥
//可以将下面的代码放到一个class中,然后调用
xxx(Model new Goods())
$goodsModel = new Goods();
$fillables = $goodsModel->getFillable();
foreach( $fillables as $fillable ) {
if( in_array($fillable , request()->all()) ) {
$map[$fillable] = request($fillable);
}
}
$goods = Goods::where($map)->select(['id' , 'name' , 'img' , 'sell_price' , 'market_price']);
//?sortby=name&order=asc
if( request('sortby') && request('order') ) {
$sort = request('sortby');
$order = request('order');
$goods = $goods->orderBy($sort , $order);
}
//?limit=10&page=0
if( request('limit') && request('page',0)>-1 ) {
$skip = (int)request('limit') * (int)request('page');
$take = (int)request('limit');
$goods = $goods->skip($skip)->take($take);
}
$goods = $goods->get();
return $goods;
$fills = $order->getFillable();//判断是否设置了fillable
$update = request()->all();
foreach( $update as $k => $v ) {
if( empty($fills) ) {
if( isset($order->$k) ) {
$order->$k = $v;
}
}else{
$order->$k = $v;
}
}
$result = $order->save();
if( !$result ) {
return response()->json(Result::toArray([] , 1 , '操作未完成'));
}
return response()->json(Result::toArray($order));
<?php
namespace App\Http\Controllers\xc\api;
use App\Classes\Result;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
/**
* Trait CommonCRUD
* @package App\Http\Controllers\xc\api
*/
trait CommonCRUD
{
protected $with = [];
protected $orderBy = '';
protected $select = ['*'];
public function commonIndex(Request $request , Model $model)
{
$sortBy = request('sortBy');
$order = request('order');
$limit = request('limit',10);
$page = request('page',0);
try {
$map = request()->all() ? request()->all() : [];
$model = $model->where($map);
if( !empty($this->with) ) {
$model = $model->with($this->with);
}
if( !empty($this->orderBy) ) {
$model = $model->orderBy($this->orderBy[0] ?: $model->primaryKey , $this->orderBy[1] ?: 'asc');
}
if( $sortBy && $order ) {
$model = $model->orderBy($sortBy , $order);
}
if( $limit && $page ) {
$model = $model->skip($limit * $page)->take($limit);
}
$model = $model->select($this->select);
$result = $model->get();
return response()->json(Result::toArray($result));
} catch( \Exception $e ) {
return response()->json(Result::toArray([] , 1 , $e->getMessage()));
}
}
public function commonUpdate(Request $request , Model $model)
{
$fills = $model->getFillable();
$update = request()->all();
foreach( $update as $k => $v ) {
if( empty($fills) ) {
if( isset($model->$k) ) {
$model->$k = $v;
}
}
else {
$model->$k = $v;
}
}
$result = $model->save();
if( !$result ) {
return response()->json(Result::toArray([] , 1 , '操作未完成'));
}
return response()->json(Result::toArray($model));
}
public function commonStore(Request $request , Model $model)
{
$data = [];
$fills = $model->getFillable();
foreach( $model->getFillable() as $key ) {
if( empty($fills) ) {
$data[$key] = $request->$key;
}
else {
if( $request->has($key) ) {
$data[$key] = $request->$key;
}
}
}
if( !$model::create($data) ) {
return response()->json(Result::toArray([]));
}
else {
return response()->json(Result::toArray([] , 1 , 'error'));
}
}
public function commonDestroy(Request $request , Model $model)
{
try {
$model->delete();
return response()->json(Result::toArray([]));
} catch( \Exception $e ) {
return response()->json(Result::toArray([] , 1 , $e->getMessage()));
}
}
}
//调用
use CommonCRUD;
public function index(Request $request)
{
$this->with = ['user'];
$this->orderBy = ['id','desc'];
$this->select = ['id','water','user_id'];
return $this->commonIndex($request , new WashOrder());
}