首页 > 解决方案 > 无法使用 Cloudinary 和 Laravel 显示图像

问题描述

我正在尝试使用 cloudinary 来保存我的图像,下面的代码可以将图像保存到 cloudinary,但无法在我的视图中显示图像。

 public function store(Request $request)
    {


    $image = $request->file('image');
    $name = $request->file('image')->getClientOriginalName();
    $destinationPath    = 'images/';
    $image = $request->file('image')->getRealPath();;

    Cloudder::upload($image, null);

   list($width, $height) = getimagesize($image);

    $image_url= Cloudder::show(Cloudder::getPublicId(), ["width" => $width, "height"=>$height]);

    .
    .
    .

     return redirect ( route ('admin.book.index'));

    }

在我的view page,我有这个

      @foreach($books as $book)
      <div class="col-1-5">
        <div class="home-catalog-image">
        <a href="{{ $book->image_url }}" target="_blank">
        <img src="{{ $book->image }}" alt="trending image" />
        </a>
        </div>
        <p class="author">{{ $book->author->name }}</p>
        <h1 class="book-title">{{str_limit($book -> name, 20) }}</h1>
      </div>
      @endforeach

我已经将它包含在我的控制器中

 public function index()
    {
        $books = Book::orderBy('created_at', 'desc')->take(10)->get();
        return view('bookpage')->with('books', $books);
    }

更新

在我的研究过程中,我更新了我的代码,但它返回错误

调用字符串上的成员函数 move()

    $image = $request->file('image');
    $name = $request->file('image')->getClientOriginalName();
    $destinationPath    = 'images/';
    $image = $request->file('image')->getRealPath();;

    Cloudder::upload($image, null);

   list($width, $height) = getimagesize($image);

    $image_url= Cloudder::show(Cloudder::getPublicId(), ["width" => $width, "height"=>$height]);

    //save to uploads directory

    $image->move(public_path("uploads"), $name);

    //Save images
    $this->saveImages($request, $image_url);

标签: laravel

解决方案


问题出在我看来

      @foreach($books as $book)
      <div class="col-1-5">
        <div class="home-catalog-image">
        <a href="{{ $book->image_url }}" target="_blank">
        <img src="{{ $book->image_url }}" alt="trending image" />
        </a>
        </div>
        <p class="author">{{ $book->author->name }}</p>
        <h1 class="book-title">{{str_limit($book -> name, 20) }}</h1>
      </div>
      @endforeach

推荐阅读