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

时间:2015-10-24 16:07:47

标签: php laravel-5 intervention

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

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

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();

1 个答案:

答案 0 :(得分:1)

您应该创建一个合适的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',
];

}

现在添加到您的Controller的商店操作:

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

我希望它有所帮助:)