繁体   English   中英

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

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

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

php artisan make:model MyImage

这将创建“ MyImage”雄辩模型及其数据库迁移。

通过将新的路径字段添加到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();

希望对您有所帮助:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM