简体   繁体   中英

Laravel blade image display

can someone help me with image display in laravel blade?

So this is full image URL after upload

http://webshop.local/uploads/products/1592949903recommend.png

It is supposed to be uploaded to

public/uploads/products folder.

Here is part of code where I create product (ProductsController):

public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required',
            'price' => 'required',
            'description' => 'required',
            'productImage' => 'required|image'
        ]);

        $productImage = $request->productImage;

        $productImage_new_name = time() . $productImage->getClientOriginalName();

        $productImage->move('uploads/products' . $productImage_new_name);

        Product::create([
            'title' => $request->title,
            'productImage' => '/uploads/products/' . $productImage_new_name,
            'price' => $request->price,
            'description' => $request->description
        ]);

        return redirect()->route('products.index');
    }

Also here is blade display:

<th><img src="{{ $product->productImage }}" alt="{{ $product->title }} image"></th>

And this is getting me right link because of:

public function getProductImageAttribute($productImage){
        return asset($productImage);
    }

in product model.

EDIT When I upload new image it does not go into folder instead it just create new folder with the name of the image and inside it is file with name phpSOMETHING_RANDOM_HERE.tmp

In your store method

$path = $request->productImage->store('uploads/products');
// OR if you need to give it name
$path = $request->productImage->storeAs('uploads/products', 'filename.jpg');

You can 'productImage' => $path, while creating product.

If anyone else faces this problem please check this part of your code.

$productImage = $request->productImage;
$productImage_new_name = time() . $productImage->getClientOriginalName();
$productImage->move('uploads/products', $productImage_new_name);

Specifically this line:

$productImage->move('uploads/products', $productImage_new_name);

The whole time I had dot between path and variable for the name of an uploaded file .

So to fix this problem just change dot for coma.

Instead of

$productImage->move('uploads/products' . $productImage_new_name);

You need

$productImage->move('uploads/products', $productImage_new_name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM