首页 > 解决方案 > laravel 使用 find 时出现此错误:调用未定义的方法 Intervention\Image\ImageManager::find()

问题描述

我正在通过刀片中的帖子关系访问图像。我想通过已有的循环删除特定图像。而且我能够将特定图像获取到函数destroyImg。但我未能在destroyImg中删除它。

当我首先尝试找到它时,我收到了这个错误:

Call to undefined method Intervention\Image\ImageManager::find()

当我按 id 杀死和转储图像时,我可以看到特定的图像。例如:

{
  "id": 79,
  "post_id": 31,
  "image": "1596537701-116796500_331884521301081_8587878113120337546_n.jpg",
  "created_at": "2020-08-04T10:41:41.000000Z",
  "updated_at": "2020-08-04T10:41:41.000000Z"
}

刀片文件

 @foreach($posts as $post)
  @for($i=0;  $i< count($image = $post->images()->get()); $i++)
      //loop has some own operations here


     <form action="/img/delete/{{$image[$i]  }}" enctype="multipart/form-data" method="POST">
   </form>
 @endforeach

后控制器

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Intervention\Image\Facades\Image;

use Carbon\Carbon;
use App\User;
use App\Post;



use Illuminate\Http\Request;

 public function destroyImg($id)
{
  dd($id);
  $try = Image::find($id);
 
   //$id->delete();
  $user = Auth::guard('web')->id();
  ($user);
  
  return redirect()->route('home',['user'=>$user]);
}

标签: phplaravel

解决方案


随着表格从更改{{$image[$i]['id'] }}为我设法通过在destroyImg函数{{$image[$i]->id }}中传递模型来使用图像模型查找。

注:图片模型为images

后控制器

 use Intervention\Image\Facades\Image;
 use App\images;

 public function destroyImg(images $images, $id)
 {
    
    $try = images::find($id); 
    $try->delete();
    
    
    $user = Auth::guard('web')->id(); 
    return redirect()->route('home',['user'=>$user]);
 }

推荐阅读