如何删除斜杠"/";在Laravel 5.2中插入数据库之前


How to remove slashes "/" before inserting into database in - Laravel 5.2

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

这就是我插入产品的方式:(我使用条带斜杠,但它什么都没做)

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>

使用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
        ]);