0

I’m working on a CRUD system for inventory management, in which images for each product should be included. Every time that I try to save the path of the image in the DB this error appears:

Undefined variable: image

My controller looks like this:

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');
    }
}

and my input looks like this:

<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>

and dd($request->all()); delivers this

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

2
  • 2
    If your code don't enter in the if ($request->hasFile('Product_Image')) conditional $image won't be initialized. That's why you are getting that error. Commented Mar 13, 2019 at 14:04
  • @namelivia I tried that at the beginning, but when I put my code in the conditional, then Image is saved in the DB as NULL Commented Mar 13, 2019 at 14:13

1 Answer 1

1

Change your code to

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');
    }
}
Sign up to request clarification or add additional context in comments.

13 Comments

The error is gone, but now the image path appears as NULL in the DB
check $request->hasFile('Product_Image')) also show dd($request->all());
dd($request->hasFile('Product_Image')); returns false
What dd($request->all()); after upload file
If you see "Product_Image" => "6.jpg". That mean file actually not uploaded.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.