如何在laravel背包的表格中显示图像

问题描述 投票:0回答:2

我在 laravel-backpack 中查看表格中的图像时遇到问题。创建图像增删改查后,仅显示文件名。但我想在表格中显示图像。

提前致谢。

laravel-5.8 laravel-backpack
2个回答
2
投票

您可以使用图像列类型。例如,在您的 ProductCrudController 中,您可以:

protected function setupListOperation()
{
    // ...

    $this->crud->addColumn([
      'name' => 'profile_image', // The db column name
      'label' => "Profile image", // Table column heading
      'type' => 'image',

      // OPTIONALS
      // 'prefix' => 'folder/subfolder/',
      // image from a different disk (like s3 bucket)
      // 'disk' => 'disk-name', 

      // optional width/height if 25px is not ok with you
      // 'height' => '30px',
      // 'width' => '30px',
    ]);

    // ...
}

0
投票

要显示图像,请先制作控制器,将图像路径存储在公共文件夹中。

@foreach($products as $product)
 @if($product->image)
  <img src="{{ asset('images/' . $product->image) }}" width="50" height="50" alt="{{ $product->name }}">
 @endif
@endforeach

//for controller below code
public function index(){
        $products = Product::all();
        return view('product.index', compact('products'));
    }

public function store(Request $request)
{
    $request->validate([
        'name' => 'required|string|max:255',
        'description' => 'required|string',
        'image' => 'image|nullable|max:1999',
    ]);

    $product = new Product();
    $product->name = $request->name;
    $product->description = $request->description;

    // Handle image upload
    if ($request->hasFile('image')) {
        $img = $request->file('image');
        $filename = time() . '.' . $img->getClientOriginalExtension();
        $path = public_path('images');
        $img->move($path, $filename); // Move to public/images
        $product->image = $filename;  
    }

    $product->save();
    return redirect()->route('products.index')->with('success', 'Product created successfully.');
}

public function update(Request $request, Product $product)
{
    $request->validate([
        'name' => 'required|string|max:255',
        'description' => 'required|string',
        'image' => 'image|nullable|max:1999',
    ]);

    $product->name = $request->name;
    $product->description = $request->description;

    if ($request->hasFile('image')) {
        // Delete the old image if exists
        if ($product->image) {
            $oldImagePath = public_path('images/' . $product->image);
            if (file_exists($oldImagePath)) {
                unlink($oldImagePath);
            }
        }

        // Upload new image
        $img = $request->file('image');
        $filename = time() . '.' . $img->getClientOriginalExtension();
        $img->move(public_path('images'), $filename);
        $product->image = $filename; // Update the image field
    }

    $product->save();
    return redirect()->route('products.index')->with('success', 'Product updated successfully.');
}

public function destroy(Product $product)
{
    // Delete the image before deleting the product
    if ($product->image) {
        $oldImagePath = public_path('images/' . $product->image);
        if (file_exists($oldImagePath)) {
            unlink($oldImagePath);
        }
    }

    $product->delete();
    return redirect()->route('products.index')->with('success', 'Product deleted successfully.');
}`
© www.soinside.com 2019 - 2024. All rights reserved.