Laravel 如何处理表单多余参数(_token,_method)?

问题 #


1.在提交表单时会随代 _token 一起提交 ( <input type='hidden' name='_token' value='...'> )
2.在修改方法时需要标注请求类型例如 ( <input type='hidden' name='_method' value='put'> )

请求后,后台数据会直接接收到 _method , _token 。 每次存入数据库时都需要unset这两个参数么?

应急方法 #


if( !function_exists('__field') ){
    function __field( $data )
    {
        $field  =   [
            "_token" , "_method"
        ];
        foreach ( $field as $item ){
            if( isset($data[$item]) ){
                unset($data[$item]);
            }
        }
        return $data;
    }
}

控制器代码

public function update(RoleSave $request, $id)
{
        RoleModel::where("id" , $id)->update(__field($request->all()));
        return success( 'SUCCESS');
}

翻了下手册本给模型加了白名单提供了允许批量跟新的字段,本以为就把问题解决了
此方法只支持 create 并不支持 update 意思就是说在 update 时如果带有表里不存在的字段一样会报错,这也算是解决了一半吧!

在屏蔽 Token 方面还有一个方法也是 @Summer 提供的VerifyCsrfToken 添加过滤条件

求解,更好的办法过滤掉多余参数!.

i@llons.com
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 12

RoleModel::where("id" , $id)->update(__field($request->all()));

改为

RoleModel::where("id" , $id)->first()->update($request->all());

这样就能用 fillable 白名单模式了. 就像楼上所说, 你的代码的update用的是 QueryBuilder 而不是 model 的 update.

不过对于laravel5来说, 直接用 注入 最方便了.

public function update(RoleSave $request, RoleModel $role)
{
        $role->update($request->all());
        return success( 'SUCCESS');
}
7年前 评论
JINJIALEI 4年前
Summer

protected $fillable = ['name'];

利用 https://learnku.com/docs/laravel/5.3/eloquent#批量赋值

7年前 评论

@Summer 批量赋值只有在 create时有效,update 无效!已试.

7年前 评论

参考 phphub.org 源码解决了:+1: !.算是借鉴了..
$allowed_fields and performUpdate function

7年前 评论

为何要直接用表单的request的数据?自己都不转一层?好可怕~!!!

7年前 评论

@5J request 有 validation 层先处理后再做入库处理 :tada:

7年前 评论

@aqyuyang 对Model 进行Update 操作 MassAssignment 会没有效果? 不太可能吧
不是说模型的话 请忽略。。。

    /**
     * Update the model in the database.
     *
     * @param  array  $attributes
     * @param  array  $options
     * @return bool
     */
    public function update(array $attributes = [], array $options = [])
    {
        if (! $this->exists) {
            return false;
        }

        return $this->fill($attributes)->save($options);
    }

调用的fill 方法

    /**
     * Fill the model with an array of attributes.
     *
     * @param  array  $attributes
     * @return $this
     *
     * @throws \Illuminate\Database\Eloquent\MassAssignmentException
     */
    public function fill(array $attributes)
    {
        $totallyGuarded = $this->totallyGuarded();

        foreach ($this->fillableFromArray($attributes) as $key => $value) {
            $key = $this->removeTableFromKey($key);

            // The developers may choose to place some attributes in the "fillable" array
            // which means only those attributes may be set through mass assignment to
            // the model, and all others will just get ignored for security reasons.
            if ($this->isFillable($key)) {
                $this->setAttribute($key, $value);
            } elseif ($totallyGuarded) {
                throw new MassAssignmentException($key);
            }
        }

        return $this;
    }
7年前 评论

我以为是 $request->except('_token', '_method') ...

7年前 评论
Cooper

$request->except('_token', '_method')

7年前 评论

@JasonXt 指的模型在批量更新时 黑白名单 在批量创建时有效,批量更新时无效!.

7年前 评论

@JasonXt 好吧 你那个其实走的不是模型 走的是 QueryBuilder

    /**
     * Update a record in the database.
     *
     * @param  array  $values
     * @return int
     */
    public function update(array $values)
    {
        $sql = $this->grammar->compileUpdate($this, $values);

        return $this->connection->update($sql, $this->cleanBindings(
            $this->grammar->prepareBindingsForUpdate($this->bindings, $values)
        ));
    }
7年前 评论

RoleModel::where("id" , $id)->update(__field($request->all()));

改为

RoleModel::where("id" , $id)->first()->update($request->all());

这样就能用 fillable 白名单模式了. 就像楼上所说, 你的代码的update用的是 QueryBuilder 而不是 model 的 update.

不过对于laravel5来说, 直接用 注入 最方便了.

public function update(RoleSave $request, RoleModel $role)
{
        $role->update($request->all());
        return success( 'SUCCESS');
}
7年前 评论
JINJIALEI 4年前

这样不就可以触发了 fillable 了?

RoleModel::find($id)->update($request->all())
6年前 评论

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