Laravel 5干预图像上传多次调整大小并保存路径到数据库


Laravel 5 intervention image upload multiple resize and save path to database

我已经能够遵循这个答案,我实际上可以创建多个图像大小。

我的问题是,如何保存每个路径到数据库。

public function store(Request $request)
{
    $input= $request->all();
    $file = $input['image'];
    $destinationPath='images/products';
    $destinationPathlarge='images/products/large';
    $extension = $file->getClientOriginalExtension();
    $fileName = rand(111,999).'.'.$extension;
    $image = $destinationPath . '/' .$fileName;
   $upload_success=  $file-> move($destinationPath,$fileName);
    $doc = new Products();
    $doc->name = $input['name'];
    $doc->price = $input['price'];
    $doc->description = $input['description'];
    $doc->s_description = $input['s_description'];
    $doc->brands_id = $input['brands_id'];
    $doc->categories_id = $input['categories_id'];
   $upload = Image::make($image)->resize(190,185)->save($destinationPath. '/' .$fileName)
        ->resize(100,100)->save($destinationPathlarge. '/'.$fileName);
    $doc->save();

您应该创建一个合适的Eloquent模型。

首先,在项目文件夹中运行一个artisan命令。

php artisan make:model MyImage

这将创建'MyImage' Eloquent Model和它的数据库迁移。

通过向up()函数添加新的路径字段来编辑新创建的迁移文件,如下所示:

Schema::create('my_images', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('path_190');
    $table->string('path_100');
    $table->timestamps();
});

运行新的迁移以对数据库进行更改。

然后,在App'MyImage模型类中,添加fillable属性以启用路径字段的填充:

class Image extends Model {
protected $fillable = [
    'path_100',
    'path_190',
];

}

现在添加到控制器的store动作:

App'MyImage::create([
    'path_100' => asset($destinationPathlarge. '/'.$fileName100),
    'path_190' => asset($destinationPathlarge. '/'.$fileName190),
])->save();

我希望这对你有帮助。