关于 Laravel Requset 的问题

关于Laravel  Requset的问题
用offsetUnset去掉这个字段之后,打印$request->all() 依旧有 password字段。
理想效果是调用all方法能过滤掉自己想过滤的字段。

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
最佳答案

如果改成这样就对了

  /**
     * Remove the value at the given offset.
     *
     * @param  string  $offset
     * @return void
     */
    public function offsetUnset($offset)
    {
        $this->getInputSource()->remove($offset);
        $this->query->remove($offset);
    }
3年前 评论
讨论数量: 11

$request->only(['字段1','字段2'])

4年前 评论
striver (楼主) 4年前
monanxiao (作者) 4年前
lochpure
$request->except(['password']);

这样吗?剔除不需要的字段

4年前 评论
striver (楼主) 4年前
lochpure (作者) 4年前
EmptyCup 4年前

,,,你为啥一定要通过这个方式去掉这个值,,,

不过你可以看看 $request->all() 的源码,看他从哪里搞来的所有数据,,,然后对症下药

4年前 评论

我试了下。unset后不会有这样的情况,看了下源码

    /**
     * Remove the value at the given offset.
     *
     * @param  string  $offset
     * @return void
     */
    public function offsetUnset($offset)
    {
        $this->getInputSource()->remove($offset);
    }
    /**
     * Get all of the input and files for the request.
     *
     * @param  array|mixed|null  $keys
     * @return array
     */
    public function all($keys = null)
    {
        $input = array_replace_recursive($this->input(), $this->allFiles());

        if (! $keys) {
            return $input;
        }

        $results = [];

        foreach (is_array($keys) ? $keys : func_get_args() as $key) {
            Arr::set($results, $key, Arr::get($input, $key));
        }

        return $results;
    }

我觉得是删掉了的。。因为从inputSource删了。

4年前 评论
striver (楼主) 4年前
sunrise丶 (作者) 4年前

之所以想剔除掉all里面的参数,是想从全局过滤用户参数,已达到不用一直传参数。 file

file

file

file

4年前 评论
DotO 4年前
striver (作者) (楼主) 4年前

今早发现。post和put方法就能过滤掉。不过只能过滤掉body里面的参数,是query里面传过来的就过滤不掉,不知道算不算bug。

4年前 评论

是的,应该是个小bug。
如果是POST,这时候offsetUnset,不会去删除查询字符串里面的参数。

file

3年前 评论

如果改成这样就对了

  /**
     * Remove the value at the given offset.
     *
     * @param  string  $offset
     * @return void
     */
    public function offsetUnset($offset)
    {
        $this->getInputSource()->remove($offset);
        $this->query->remove($offset);
    }
3年前 评论

@di-gua @striver 不是 bug 哟,你们思路错了

$request->request->remove('status');
3年前 评论
JerryBool 3年前
JerryBool 3年前
王成涛 (作者) 3年前
JerryBool 3年前

file

file

3年前 评论
JerryBool 3年前
striver (楼主) 3年前
王成涛 (作者) 3年前
JerryBool 3年前
JerryBool 3年前
striver (楼主) 3年前
JerryBool 3年前

offsetUnset 方法是 ArrayAccess 接口的方法的实现,可以把对象像数组一样进行访问。正确的使用方法是这样的:

unset($request['password']);

至于为什么没有过滤掉 password,可以断点调试下源码。

3年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!