验证或删除laravel中的其他字段 [英] Validate or remove for extra fields in laravel

查看:157
本文介绍了验证或删除laravel中的其他字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用其他字段的规则来验证请求,或者从请求中删除该字段?

It's posible to validate request with rules for additional fields, or remove that fields from request?

简单的例子,我有带有规则的FormRequest对象:

Simple example, I have FormRequest object with rules:

public function rules() {
        return [
            'id' => 'required|integer',
            'company_name' => 'required|max:255',
        ];
    }

当我收到其他任何字段的发帖请求时,我想在控制器中获取错误/异常,或者我只想获取id和company_name字段,而没有其他任何字段. laravel具有任何功能,还是我必须以自己的方式实现?

And when I get post request with any other fields I want to get error/exception in controller or I want to get only id and company_name fields, with no any others. It's exist any feature in laravel with that, or I must make it in my way?

推荐答案

与laravel一起发布的请求不仅包含您的表单输入,还包含更多内容,因此您需要检查Request以查看其中的内容.

Your post request with laravel has more stuff than just your form input so you need to check Request to see what is inside.

要只获取所需的字段,可以使用:

To get only the fields you want you can use:

$request->only(['fieldname1', 'fieldname2', 'fieldname3']);

$request->except(['fieldnameYouDontWant1', 'fieldnameYouDontWant2', 'fieldnameYouDontWant3']);

排除不需要的内容.

这篇关于验证或删除laravel中的其他字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆