doubiaozhan0745 2014-10-30 16:07
浏览 24

输入消毒Laravel 4

I am using Laravel 4 to create a RESTful interface for my AngularJS app.

At the moment, I want to update an object. My model is called Discount Link The way I do this is:

    $data = Input::all();
    $affectedRows = DiscountLink::where('id', '=', $id)->update($data);

(Please imagine there are some kind of validation checks)

I get an error in my laravel.log:

General error: 1 no such column: $edit

Edit is a value that was passed to my server because it was added in by angular. I don't see the need to explicitly remove this in my JS so my question is:

How can I create some kind of whitelist of key-names that my model will pay attention to. That way, even if something get's passed in by accident, the REST call doesn't have to fail.

I guess there are arguments for sanitising in the JS but i will then pollute my code with many de-assignment statements which could become messy, especially when dealing with AngularJS:

delete discountLink.$edit;

Any suggestions?

  • 写回答

1条回答 默认 最新

  • dsn1327 2014-10-30 16:33
    关注
    $data = Input::all();
    $discountLink = DiscountLink::find($id);
    $discountLink->fill($data);
    $discountLink->save();
    

    This method is working. But your fillable/guarded attribute must be set correct.

    UPDATE:

    New method:

    $data = Input::all();
    $data['id'] = $id;
    $genre = new Genre;
    $genre->exists = true;
    $genre->update($data);
    

    It's 1 query, as I know.

    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误