簡體   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