1.在Controller文件夹下创建GetwayController.php文件(可自定义controller下任意位置及文件名)
<?php
namespace App\Http\Controllers\Home;
use App\Http\Controllers\Controller;
use App\Models\Processdata\Insert;
use App\Models\Processdata\Select;
use App\Models\Processdata\Update;
use Illuminate\Support\Facades\Auth;
class GetwayController extends Controller
{
public static function forway($data,$way)
{
$select = new Select();
$insert = new Insert();
$update = new Update();
switch ($way) {
case 'GET' :
return $select->selects($data); //查询方式
break;
case 'POST' :
return $insert->inserts($data); //新建
break;
case 'DELETE' :
return $update->deleteds($data); //删除
break;
case 'PATCH' :
return $update->updateds($data); //修改
break;
default:
return $select->selects($data);
break;
}
}
}
2.在App\Models\Processdata文件夹下创建一下文件
Insert.php
<?php
namespace App\Models\Processdata;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class Insert extends Model
{
public function insertor($data){
$table = $data['table'];
$data['created_at'] = date('Y-m-d H:i:s');
$data['updated_at'] = date('Y-m-d H:i:s');
$unset = ['table','relations','key_words','_token'];
foreach($unset as $k){
unset($data[$k]);
}
$result = DB::table($table)->insert($data);
return response()->json(['code'=>200,'message'=>'添加成功','data'=>'','success'=>true]);
}
public function inserts($data){
$table = $data['table'];
$data['created_at'] = date('Y-m-d H:i:s');
$data['updated_at'] = date('Y-m-d H:i:s');
$unset = ['table','relations','key_words','_token','copy','id'];
foreach($unset as $k){
unset($data[$k]);
}
// dd($data);
$result = DB::table($table)->insert($data);
// dd($result);
if(!$result){
return '添加失败';
}
return '添加成功';
}
}
Select.php
<?php
namespace App\Models\Processdata;
use App\Models\Morder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class Select extends Model
{
public function selects($data){
$arr = ['datetime','order','table','current','_token','field'];
foreach($arr as $key=>$vo){
if(array_key_exists($vo,$data)){
$info[$vo] = $data[$vo];
unset($data[$vo]);
}
}
$data = array_filter($data);
$field = null;
if(!empty($info['datetime'])){
$datetime = explode(",",$info['datetime']);
}
if(!empty($info['order'])){
$order = explode(",",$info['order']);
}
$where = [];
if(isset($data['like'])){
$where[] = $data['like'];
unset($data['like']);
}
if(!empty($info['field'])){
$field = explode(",",$info['field']);//转换成数组
}
foreach ($data as $k=>$v){
count($data) != 0 && $where[] = [$k,$v];
}
$result = DB::table($info['table'])->where($where)->orderBy('id','desc')->get($field);
if(!empty($order)){
$result = DB::table($info['table'])->where($where)->orderBy($order[0],$order[1])->orderBy('id','desc')->get($field);
}
if(!empty($datetime[1]) && !empty($datetime[2])){
$datetime[1] = $datetime[1]." 00:00:00";
$datetime[2] = $datetime[2]." 23:59:59";
$result = DB::table($info['table'])->where($where)->whereBetween($datetime[0],[$datetime[1],$datetime[2]])->orderBy('id','desc')->get($field);
if(!empty($order)){
$result = DB::table($info['table'])->where($where)->whereBetween($datetime[0],array($datetime[1],$datetime[2]))->orderBy($order[0],$order[1])->orderBy('id','desc')->get($field);
}
}
return $result;
}
}
Update.php
<?php
namespace App\Models\Processdata;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class Update extends Model
{
public function updateds($data){
if(!empty($data['id'])){
$info['id'] = intval($data['id']);
}
if(!empty($data['select'])){
$info['type'] = $data['select'];
}
$delete = !empty($data['delete'])?$data['delete']:false;
// $info['id'] = intval($data['id']);
$table = $data['table'];
$data['updated_at'] = date('Y-m-d H:i:s');
$unset = ['table','id','_method','relations','key_words','_token','select','delete'];
foreach($unset as $k){
unset($data[$k]);
}
$result = DB::table($table)->where($info)->update($data);
if($result){
$msg = '修改成功';
}else{
$msg = '修改失败';
}
if(!empty($delete)){
if($result){
$msg = '已删除';
}else{
$msg = '删除失败';
}
}
return $msg;
}
public function deleteds($data){
$table = $data['table'];
$unset = ['table','_method','relations','key_words','_token','delete','copy'];
foreach($unset as $k){
unset($data[$k]);
}
$result = DB::table($table)->where($data)->delete();
// dd($result);
if($result == 0){
return '删除失败';
}
return '已删除';
}
}
3.调用
引入
use App\Http\Controllers\Home\GetwayController;
使用
$data = $request->all();
$data['table'] = 'pro_classify';
$result=GetwayController::forway($data,'GET');
$result=$result[0];
if(!empty($request->all())) {
$data = $request->all();
$data['table'] = 'pro_classify';
//执行添加
if(empty($data['id'])){
$result=GetwayController::forway($data,'POST');
return response()->json(['message'=>'添加成功','error'=>0,'data'=>$result]);
}else{
// dd($data);
if($data['pid'] != '0'){
if(!empty($data['hide'])){
$data['hide'] = '2';
}else{
$data['hide'] = '1';
}
}
// dd($data);
//执行修改
$result=GetwayController::forway($data,'PATCH');
return response()->json(['message'=>'修改成功','error'=>0,'data'=>$result]);
}
}
网友评论