将图像路径存储在数据库中

时间:2019-03-13 14:01:34

标签: laravel

我正在使用CRUD系统进行库存管理,其中应包含每种产品的图片。每当我尝试将图像的路径保存在数据库中时,都会出现此错误:

  

未定义变量:图片

我的控制器如下:

public function store(Request $request)
{
    if (Auth::user('logistics')) {
        $product = $this->validate(request(), [
            'Product_Name' => 'required',
            'Amount'       => 'required|numeric',
            'MinAmount'    => 'required|numeric',
            'Status'       => 'required',
            'Supplier'     => 'required',
            'WebLink'      => 'required',
        ]);
        if ($request->hasFile('Product_Image')) {
            $image = Storage::putFile('public/pictures/LogInv/', $request->Product_Image);
        }
        $product['Product_Image'] = $image;
        $product['Employee_id'] = Auth::user()->id;
        LogisticsInv::create($product);

        return back()->with('success', 'Product has been added');
    } else {
        return view('/restricted_area');
    }
}

我的输入看起来像这样:

<form method="post" action="{{url('loginv')}}" enctype="multipart/form-data">
    {{csrf_field()}}
    <div class="row">
        <div class="col-md-12"></div>
        <div class="form-group col-md-12">
            <label for="Product_Image">Product Image:</label>
            <input type="file" class="form-control" name="Product_Image">
        </div>
    </div>

dd($request->all());交付

  

array:8 [▼“ _token” =>“ P7m8GP4A35G1ETUosduBSWtMpJuPaNILn2WI6Al3”
  “ Product_Image” =>“ 6.jpg”“ Product_Name” =>“ asd”“ Amount” =>   “ 123”“ MinAmount” =>“ 1”“ Status” =>“ Ok”“ Supplier” =>“ asd”
  “ WebLink” =>“ asd”]

1 个答案:

答案 0 :(得分:1)

将代码更改为

public function store(Request $request)
{
    if (Auth::user('logistics')) {
        $product = $this->validate(request(), [
            'Product_Name' => 'required',
            'Amount' => 'required|numeric',
            'MinAmount' => 'required|numeric',
            'Status' => 'required',
            'Supplier' => 'required',
            'WebLink' => 'required'
        ]);
        if ($request->hasFile('Product_Image')) {
            $image = Storage::putFile('public/pictures/LogInv/', $request->Product_Image);
            $product['Product_Image'] = $image;
        }
        $product['Employee_id'] = Auth::user()->id;
        LogisticsInv::create($product);
        return back()->with('success', 'Product has been added');
    } else {
        return view('/restricted_area');
    }
}