如何删除斜线" /"在插入数据库之前--Laravel 5.2

时间:2016-04-14 18:41:00

标签: php mysql laravel laravel-5.2

我按标题显示我的产品,有时我的一个产品会有一个" /"在它的标题。每次我点击产品详细信息时,都会给我一个错误,因为URL中有一个斜杠。所以我需要删除斜杠,或者在插入后端数据库之前显示错误信息。

这是我插入产品的方式:(我使用 stripslashes ,但它没有做任何事情)

public function addPostProduct(ProductRequest $request) {


        // Create the product in DB
        $product = Product::create([
            'product_name' => stripslashes($request->input('product_name')),
        ]); 


        // Save the product into the Database.
        $product->save();

        // Flash a success message
        flash()->success('Success', 'Product created successfully!');

        // Redirect back to Show all products page.
        return redirect()->route('admin.product.show');
    }

这是我的请求检查:

class ProductRequest extends Request {

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules() {
        return [
            'product_name' => 'required|max:75|min:3|unique:products',
        ];
    }

}

这是我按标题显示单个产品的途径。 (我试过{!! !!},但它也没有工作)

 <a href="{!!  route('show.product', $product->product_name) !!}">Products Show</a>

1 个答案:

答案 0 :(得分:2)

使用str_replace

$title = "product/124"; 

echo stripslashes($title) . "<br />"; 

echo str_replace("/", "", $title). "<br />"; 

或进一步澄清:

      $product_name =  str_replace("/", "",$request->input('product_name'));
 // Create the product in DB
        $product = Product::create([
            'product_name' => $product_name
        ]);