接口( )

////////////////////控制器
<?php

namespace App\Http\Controllers;

use App\models\registerModel;

// 1 先引入Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Validator;

use Illuminate\Http\Request;

class registerController extends Controller
{

public function index()
{
//
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
// $params= $request->all();
//// var_dump($params);
// $name=$request->input('name');
// if (empty($name)){
// //接口三要素
// $arr['status']=500;
// $arr['info']='用户名不可以为空';
// echo json_encode($arr);
// }
//$password=$request->input('password');
//if (empty($password)){
// $arr['status']=500;
// $arr['info']='密码不可以为空';
// echo json_encode($arr);
//
//}
//$img=$request->img;
//if (empty($img)){
// $arr['status']=500;
// //提示信息
// $arr['info']='照片不可以为空';
// //返回 json
// echo json_encode($arr);
//}
// $res=registerModel::store($name,$password,$img);
$param=$request->all();
// var_dump($param);
// $this->validate()
$validator=Validator::make($param,[
"name"=>'required',
"password"=>"required",
"img"=>"required"
],[
"name.required"=>"用户名不可以为空",
"password.required"=>"密码不可以为空",
"img.required"=>"照片不可以为空",
]);
//print_r($validator);
if ($validator->fails()){
$err=$validator->errors()->first();
$arr['status']=200;
$arr['info']=$err;
echo json_encode($arr);
}
$param['img'] = '/'.$request->img->store('img');
$res=registerModel::store($param);
if ($res){
$arr['status']=200;
$arr['info']='添加成功';
return json_encode($arr);
}else{
$arr['status']=500;
$arr['info']='添加失败';
return json_encode($arr);
}
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

//////////////////////////////////////模型
<?php

namespace App\models;

use Illuminate\Database\Eloquent\Model;
use phpDocumentor\Reflection\Types\Self_;

class registerModel extends Model
{
//定义数据表与模型的关系
protected $table='register';
public $timestamps=false;
public $PrimaryKey='id';

//添加数据库

public static function store($param){
$obj=new self();
$obj->name=$param['name'];
$obj->password=md5($param['password']);
$obj->img=$param['img'];
return $obj->save();

}





}







posted @   王越666  阅读(44)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示