我的laravel第一个项目学习到的初级知识

这篇博客主要介绍了在Laravel项目中进行数据验证的基本知识,包括required、unique等规则,并探讨了路由的常用格式和控制器的功能,如index()、store()等。同时,还详细讲解了校验目录的位置以及仓库和模型的相关路径及操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

校验

(1)自我总结

  1. required:非空
  2. unique:数据唯一
  3. string:必须是字符串类型
  4. integer:必须是int类型
  5. numeric:必须是数字
  6. array:必须是数组类型
  7. date:必须是时间类型
  8. digits_between:必须在 :min 和 :max 位之间’
  9. digits:值1,值2 在两个值长度之间
  10. between:值1,值二 在两个值大小之间
  11. max:最大长度
  12. min:最小长度
  13. regex:/^1[3456789][0-9]{9}$/: 手机号正则校验

(2)项目中校验目录

路径:vendor/laravel/lumen-framework/resources/lang/en/validation.php

(3)校验的方法

$validator = $this->getValidationFactory()->make($request->all(), [
            //需要逗号隔开每条校验
            '校验字段名' => '校验1|校验2',
            '校验字段名' => '校验1|校验2',
        ]);

        if ($validator->fails()) {
            return $this->result(-1, $validator->errors()->first());
        }

路由

(1)常用格式

$app->group(['prefix'=>'api/v1','namespace'=>'路径','middleware'=>['中间件1','中间件2','中间件3']],function($app){
	//get常用于列表   
	$app->get('路由名','控制器@方法');  
	//post常用于添加功能
	$app->post('路由名', '控制器@方法');
    //put常用于修改功能 通过id修改
    $app->put('路由名/{id}', '控制器@方法');
    //get id  常用于通过id获取数据
    $app->get('路由名/{id}', '控制器@方法');
    //delete 通过id删除
    $app->delete('路由名/{id}', '控制器@方法');
    //批量删除 传入后台的是一个数组
    $app->delete('路由名', '控制器@方法');      
});

(2)路由文件路径

路径:app/http/routes.php

控制层

(1)控制器

>1.控制器路径

路径:app/http/controllers

>2.控制器构造器

public function __construct(仓库类 $Repository)
    {
    	//中间件
        $this->middleware('pageOrder', ['only' => ['index']]);
        //通过一个全部变量使用仓库的方法
        $this->repository = $userRepository;
    }

>3.列表功能 index()

public function index(Request $request)
    {
    	//调用仓库方法
        $res = $this->repository->index($request);
        //返回成功信息
        return $this->result(0, '获取成功', $res);
    }

>4.添加功能 store()

public function store(Request $request)
    {
    	//校验内容
        $validator = $this->getValidationFactory()->make($request->all(), []);
        if ($validator->fails()) {
            return $this->returnMsg(-1,$validator->errors()->first());
        }
        //调用仓库方法实现功能
        return $this->repository->store($request) ? $this->result(0,'添加成功') : $this->result(-1, '添加失败');
    }

>5.修改功能 save()

public function save(Request $request, $id)
    {
    	//校验
        $validator = $this->getValidationFactory()->make($request->all(), []);
        if ($validator->fails()) {
            return $this->result(-1, $validator->errors()->first());
        }
        //调用仓库方法进行传值修改内容
        return $this->repository->save($request, $id) ? $this->result(0,'编辑成功') : $this->result(-1, '编辑失败');
    }

>6.删除功能 destory()

public function destory($id)
    {
        return $this->result(0, '删除成功',$this->repository->destroy($id););
    }

>7.通过id查询数据 show()

public function show($id)
    {
        return $this->result(0, '成功', $this->repository->show($id));
    }

>8.批量删除 destoryAll()

public function destoryAll(Request $request){

        return $this->result(0,'删除成功',$this->repository->destoryAll($request));
    }

(2)仓库

>1.仓库路径

路径:app/Repository

>2.仓库构造器

public $model;
public function __construct(模型 $Reviewers)
    {
        $this->model = $storeReviewers;
        parent::__construct();
    }

>3.列表功能

//列表
    public function index($request)
    {
    	//分页功能
        $cron['limit'] = $request['limit'];
        
        //排序功能
        $cron['order'] = $request['order'];
        //需要排序的字段
        $cron['order']['orderby'] = 'sort';
        //升序 asc 还是  降序 desc
        $cron['order']['orderdir'] = 'desc';
        
        //只查询某些字段
        $cron['select'] = ['字段1', '字段2', '字段3'...];
        
        //模糊查询
        $where['like'] = ['字段名'...];
        $cron['like'] = $this->getWhere($where['like'], $request);
		
		//字段数据匹配
		$where['eq'] = ['字段名1', '字段名2'...];
        $cron['where'] = $this->getWhere($where['eq'], $request);
        
        //执行拼接查询功能
        return $this->Search($this->model, $cron);
    }

Search方法

public function Search($res, $cron = array(), $draw = 0, $sumarr = array())
    {
        $sum_count = $res->count();
        if (!empty($cron['where'])) {
            $res = $res->where($cron['where']);
        }
        if (!empty($cron['In'])) {
            foreach ($cron['In'] as $k => $v) {
                $res = $res->whereIn($k, $v);
            }
        }

        if (!empty($cron['nIn'])) {
            foreach ($cron['nIn'] as $k => $v)
                $res = $res->whereNotIn($k, $v);//
        }

        if (!empty($cron['neq'])) {
            foreach ($cron['neq'] as $k => $v) {
                $res = $res->where($k, '!=', $v);
            }
        }

        if (!empty($cron['between'])) {
            foreach ($cron['between'] as $k => $v) {
                $res = $res->whereBetween($k, $v);
            }
        }

        if (!empty($cron['like'])) {
            $res = $res->where(function ($query) use ($cron) {
                foreach ($cron['like'] as $lk => $lv)
                    $query->where($lk, 'like', '%' . $lv . '%');
            });
        }

        if (!empty($cron['likeor'])) {
            $res = $res->where(function ($query) use ($cron) {
                foreach ($cron['likeor'] as $lk => $lv) {
                    $query->orWhere($lk, 'like', '%' . $lv . '%');
                }
            });
        }

        if(!empty($cron['operation'])){
            if(!empty($cron['operation_type'])){
                $res = $res->where(function($query) use($cron){
                    foreach($cron['operation'] as $v){
                        $operation_type = '=';
                        switch($v['type']){
                            case 'gt'://大于
                                $operation_type = '>';
                                break;
                            case 'egt'://大于等于
                                $operation_type = '>=';
                                break;
                            case 'lt'://小于
                                $operation_type = '<';
                                break;
                            case 'elt'://小于等于
                                $operation_type = '<=';
                                break;
                        }
                        $query->where($v['key'],$operation_type,$v['value']);
                    }
                });
            }
        }

        if (!empty($cron['limit'])) {
            $res = $res->skip($cron['limit']['page'])->take($cron['limit']['pageSize']);
        }
        if (!empty($cron['order'])) {
            $res = $res->orderBy($cron['order']['orderby'], $cron['order']['orderdir']);
        }

        if (!empty($cron['join'])) {
            foreach ($cron['join'] as $lk => $v) {
                $res = $res->leftJoin(Prefix . $v['table'], Prefix . $v['table'] . '.' . $v['left'], '=', $v['right']);
            }
        }

        if (!empty($cron['select'])) $res->select($cron['select']);

        $result = $res->get();
        if ($result->isEmpty()) {
            $data = [];
        } else {
            $data['data'] = $result;
            $data['iTotalDisplayRecords'] = $cron['limit']['pageSize'];
            $data['iTotalRecords'] = $sum_count;
        }
        return $data;
    }

>4.添加功能

public function store($request)
    {
    //给什么字段的数据  进行遍历匹配 就在数据库添加什么字段的数据
        $insert = [];
        foreach ($this->model->getFillable() as $v) {
            if (isset($request[$v])) $insert[$v] = $request[$v];
        }

        return $this->model->insert($insert);
    }

>5.修改功能

public function save($request, $id)
    {
    //给什么字段的数据  进行遍历匹配 就在数据库修改什么字段的数据
        $update = [];
        foreach ($this->model->getFillable() as $v) {
            if (isset($request[$v])) $update[$v] = $request[$v];
        }
        
        return $this->model->where('id', $id)->update($update);
    }

>6.删除功能

public function destroy($id)
    {
        return $this->model->where('id', $id)->delete();
    }

>7.通过id查询数据

public function show($id)
    {
        return $this->model->where('id', $id)->first();
    }

>8.批量删除

//批量删除
    public function destoryAll($request){

        return $this->model->whereIn('id', $request['goods_ids'])->delete();
    }

模型

(1)模型路径

路径:App\Models

(2)模型内部代码

class Brand extends MallBaseModel
{
	//是否开启时间戳  true  false
    public $timestamps = false;
    //绑定的数据库表
    protected $table = '表名';
    //隐藏的字段
    protected $hidden = ['updated_at'];
	//白名单 需要的字段
    protected $fillable = ['字段1', '字段2', '字段3', '字段4'...];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值