控制器
<?php
namespace App\Http\Controllers;
use App\Test1;
use Illuminate\Http\Request;
use Validator;
class Test extends Controller
{
//注册
public function regData(Request $request){
//验证器验证
$validator = Validator::make($request->all(), [
'name' => 'required',
'pwd' => 'required',
'phone' => 'required',
'qq' => 'required',
]);
if ($validator->fails()) {
$msg=$validator->errors()->first();
return ['code'=>201,'msg'=>$msg,'ruselt'=>null];
}
//拼写入库的数组
$arr['name']=$request['name'];
$arr['pwd']=md5($request['pwd']);
$arr['phone']=$request['phone'];
$arr['qq']=$request['qq'];
$data=Test1::regData($arr);
if ($data){
return ['code'=>200,'msg'=>'注册成功','ruselt'=>$data];
}else{
return ['code'=>201,'msg'=>'注册失败','ruselt'=>null];
}
}
//登录
public function login(Request $request){
//验证器验证
$validator = Validator::make($request->all(), [
'name' => 'required',
'pwd' => 'required',
]);
if ($validator->fails()) {
$msg=$validator->errors()->first();
return ['code'=>201,'msg'=>$msg,'ruselt'=>null];
}
//拼写登录的数组
$arr['name']=$request['name'];
$arr['pwd']=md5($request['pwd']);
$arr2['qq']=$request['name'];
$arr2['pwd']=md5($request['pwd']);
$data=Test1::loginData($arr,$arr2);
if ($data){
return ['code'=>200,'msg'=>'登录成功','ruselt'=>$data];
}else{
return ['code'=>201,'msg'=>'登录失败','ruselt'=>null];
}
}
//展示
public function lists(){
$data=Test1::listData();
if ($data){
return ['code'=>200,'msg'=>'展示成功','ruselt'=>$data];
}else{
return ['code'=>201,'msg'=>'展示失败','ruselt'=>null];
}
}
//删除
public function delData(Request $request){
$data=Test1::delData($request['id']);
if ($data){
return ['code'=>200,'msg'=>'删除成功','ruselt'=>$data];
}else{
return ['code'=>201,'msg'=>'删除失败','ruselt'=>null];
}
}
//详情
public function infoData(Request $request){
$data=Test1::infoData($request['id']);
if ($data){
return ['code'=>200,'msg'=>'查看详情成功','ruselt'=>$data];
}else{
return ['code'=>201,'msg'=>'查看详情失败','ruselt'=>null];
}
}
//修改
public function saveData(Request $request){
//验证器验证
$validator = Validator::make($request->all(), [
'name' => 'required',
'pwd' => 'required',
'phone' => 'required',
'qq' => 'required',
'id' => 'required',
]);
if ($validator->fails()) {
$msg=$validator->errors()->first();
return ['code'=>201,'msg'=>$msg,'ruselt'=>null];
}
//拼写修改的数组
$id=$request['id'];
$arr['name']=$request['name'];
$arr['pwd']=md5($request['pwd']);
$arr['phone']=$request['phone'];
$arr['qq']=$request['qq'];
$data=Test1::saveData($id,$arr);
if ($data){
return ['code'=>200,'msg'=>'修改成功','ruselt'=>$data];
}else{
return ['code'=>201,'msg'=>'修改失败','ruselt'=>null];
}
}
}
模型层
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Test1 extends Model
{
protected $table='test';
public $timestamps=false;
//注册方法
static public function regData($arr){
return self::insert($arr);
}
//登录
static public function loginData($arr,$arr2){
$res=self::where($arr)->first();
$res2=self::where($arr2)->first();
if (empty($res)&&empty($res2)){
return 0; //登录失败
}else{
if ($res) return $res;
if ($res2) return $res2;
}
}
//展示
static public function listData(){
return self::get();
}
//删除
static public function delData($id){
return self::where('id','=',$id)->delete();
}
//详情
static public function infoData($id){
return self::where('id','=',$id)->first();
}
//修改
static public function saveData($id,$arr){
return self::where('id','=',$id)->update($arr);
}
}
路由
Route::post('regadd','Test@regData');//注册
Route::post('loginadd','Test@login');//登录
Route::get('list','Test@lists');//展示
Route::get('del','Test@delData');//删除
Route::get('info','Test@infoData');//详情
Route::post('save','Test@saveData');//修改