简体   繁体   English

Laravel刀片图像显示

[英]Laravel blade image display

can someone help me with image display in laravel blade?有人可以帮我在 laravel 刀片中显示图像吗?

So this is full image URL after upload所以这是上传后的完整图像 URL

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

It is supposed to be uploaded to应该上传到

public/uploads/products folder. public/uploads/products文件夹。

Here is part of code where I create product (ProductsController):这是我创建产品(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.在产品 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编辑当我上传新图像时,它不会 go 到文件夹中,而是它只是创建具有图像名称的新文件夹,其中包含名称为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.您可以在创建产品时'productImage' => $path,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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