如何防止Laravel中的SQL注入?

问题描述:

在我的控制器中,我有以下代码:

In my controller I have this code:

public function create($brand_id)
{
    Brand::findOrFail($brand_id);
}

和这个:

public function search() 
{
    $q = Input::get('q');
    $brands = Brand::where('title', 'LIKE', '%'.$q.'%')->take(80)->get();

此代码安全吗? 安全"是指SQL注入安全.还是应该在这里进行一些变量清理?清理用户输入的最佳方法是什么? 非常感谢您的帮助:)

Is this code safe? By "safe" I mean SQL injection safe. Or should I do some variable clean up here? And what is the best way for cleaning up user input? Thanks a lot for helping me :)

是,Eloquent在后台使用参数绑定,可以安全地转义where()中使用的任何输入.

yes Eloquent uses parameter binding behind the scene, which safely escapes any input used in where().