I want to use images of products that I register in the database - Laravel

Viewed 137

I programmed a product registration. The registration for the database is working correctly. My problem is that I can't show the images that I registered in the database. I created an imput where the name of the image is inserted. This name is saved in the database and the image is saved with the same name, however it is saval in public. The images are inside the public / storage / products folder.

Controller:

public function index()
{   
    $products = Product::paginate(10);

    return view('products.index', [
        'products' => $products,
    ]);
}


public function store(Request $request)
{   
    // Create registration 
    $data = $request->only('name', 'price', 'imageName');

    Product::create($data);

    
    // Image
    
    if($request->file('imageProduct')->isValid()){
        $nameFile = $request->imageName . '.' . $request->file('imageProduct')->getClientOriginalExtension();
        $request->file('imageProduct')->storeAs('products', $nameFile);
        
        return redirect()->route('ProductControllerIndex');
    }

}

view:

 <div>
    @foreach ($products as $product)
        <p>
            Id: {{ $product->id }}
        </p>

        <p>
            Nome do produto: {{ $product->name }}
        </p>

        <p>
            Preço: {{ $product->price }}
        </p>

        <p>
           {{ $product->imageName }} 
        </p>

        <p>
            <img src="{{ asset('storage/products/'.$product->imageName) }}" alt="">
        </p>
        <hr>
    @endforeach
</div>
1 Answers

The core issue here is that your Image's extension is not being saved to the database, so $product->imageName, when used in the asset() helper, doesn't generate a complete URL for the image. You'll need to refactor your code a little to get it to save:

public function store(Request $request) {   
    $nameFile = $request->input('imageName', '');
    if($request->file('imageProduct')->isValid()){
        $nameFile .= '.' . $request->file('imageProduct')->getClientOriginalExtension();
        $request->file('imageProduct')->storeAs('products', $nameFile);
    }

    $request->merge(['imageName' => $nameFile]);
    $data = $request->only('name', 'price', 'imageName');

    Product::create($data);

    return redirect()->route('ProductControllerIndex');
}

In the above code, the value for $nameFile is defaulted to the value in $request->input('imageName'), or an empty string '' if nothing is supplied. Next, if a valid image is uploaded, the $nameFile variable is appended with the extension. Lastly, the $request variable is updated with the name value for imageName. The remainder of the code creates the new Product with the data supplied (using the ->only() modifier) and redirect as required.

The rest of your code should be ok, as long as the file exists in the correct directory after ->storeAs() and the fully-qualified image name is saved to the database.

Note: If for whatever reason Product::create() doesn't work with this approach, you can use the new Product() ... $product->save() approach: (there might be an issue with $request->merge() using an existing key, as I can't actually test that)

$product = new Product();

$product->name = $request->input('name');
$product->price = $request->input('price');
$product->imageName = $fileName;

$product->save();
Related