如何将模型传递到作业/队列以操纵图像



我有一个光磁盘,可以扫描目录和foreach图像在数据库中创建新记录,并派遣工作来操纵图像,但无法使工作工作!

这里的照片模型:

namespace App;
use IlluminateDatabaseEloquentModel;
class Photo extends Model
{
    protected $table = "photos";
    protected $fillable = [
        'org_path'
    ];
}

这里的光电控制器:

namespace AppHttpControllers;
use AppPhoto;
use AppJobsProcessImage;

class PhotoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    public function index()
    {
        // Get Photos inside the private folder org_folder
        $org_images = preg_grep('~.(jpeg|jpg|png)$~', scandir(storage_path('app/images/')));
        foreach ($org_images as $image) {
            $post = new Photo;
            $post->org_path = storage_path('app/images/').$image;
            $post->pub_path = NULL;
            $post->save();
            $this->dispatch(new ProcessImage($post));
        }
    }
}

在这里工作:

namespace AppJobs;
use IlluminateBusQueueable;  
use IlluminateQueueSerializesModels;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
use Image;
use AppPhoto;
class ProcessImage implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable,     SerializesModels;
    protected $post;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Photo $post)
    {   
        $this->post = $post;
    }
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {   
        $resized_image_path = storage_path('app/public/').rand(5, 100).'.jpg';
        $image = Image::make($post->org_path);
        $image->resize(200,200)->save($resized_image_path);
    }
}

我无法以某种方式访问作业的图像。你能告诉我我缺少什么吗?

您应该在handle()方法的第二行上使用$this->post访问post对象,而不是您在构造函数上分配的$post。希望解决这个问题。

最新更新