[英]upload images and store urls on database with laravel
我想上傳多個圖像並將其URL存儲到數據庫中。
這是我的代碼:
<form method="POST" action="{{ route('validated', ['id' => $product->id]) }}) }}">
@csrf
<input type="hidden" name="ID" name="ID" value="{{ $product->id }}">
<div class="form-group row">
<label for="Image" class="col-sm-2 col-form-label">Image</label>
<img src="{{ $product->imageURL }}" />
</div>
<div class="form-group row">
<div class="col-sm-12">
<button type="submit" class="btn btn-lg btn-success">Valider les données</button> | <a href="#">Relancer le fournisseur</a>
</div>
</div>
</form>
並進入我的控制器功能:
$imagePath = Storage::putFile('uploads/image/', $request->image, "imagename");
我有這個錯誤:
出現Symfony \\ Component \\ Debug \\ Exception \\ FatalThrowableError,並顯示消息“在字符串上調用成員函數hashName()”
Stacktrace:0在C:\\ laragon \\ www \\ MyProject \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Filesystem \\ FilesystemAdapter.php:208中的Symfony \\ Component \\ Debug \\ Exception \\ FatalThrowableError
如果您使用的是Laravel,則應該能夠執行以下操作:
$imagePath = $request->file('image')->store('uploads/image');
或者,如果您想指定文件名,則可以執行以下操作:
$imagePath = $request->file('image')->storeAs('uploads/image', 'myfilename.jpg');
更新
我注意到,在您的HTML表單中,您沒有文件上傳輸入。 如果您沒有文件,Laravel將沒有文件可存儲。
嗨,我認為您需要使用putFileAs方法來自定義名稱,例如:
//Manually specify a file name...
Storage::putFileAs('photos', new File('/path/to/photo'), 'photo.jpg');
此方法接受Illuminate \\ Http \\ File或Illuminate \\ Http \\ UploadedFile實例,並將自動將文件流式傳輸到所需位置:
對於多張圖片上傳,您可以嘗試以下代碼 :
$images = $request->file('images');
foreach ($images as $key => $image) {
if ($request->hasFile('images') && $request->file('images')[$key]->isValid()) {
$urlPath = $request->images[$key]->store('public/images');
$image = new Images();
$image->path = $urlPath;
$image->save();
} else {
return redirect()->back()->withInput()->with('errors', 'Invalid Image file found!');
}
}
假設您必須在enctype="multipart/form-data"
添加enctype="multipart/form-data"
,並在函數參數中Request $request
,並在公共目錄中storage link
。
希望對您有所幫助。 謝謝
首先,如果要上傳圖像,則表單必須具有enctype="multipart/form-data"
屬性。
根據Laravel文檔,您可以使用以下代碼:
$path = $request->file('image')->store('image');
如果要編寫可重用的代碼並以不同的方式使用它,請執行此操作
例如我們在Image模型中有Post和Image模型,請添加此代碼
public function post()
{
return $this->belongsTo(Post::class);
}
public static function named($name,$type)
{
$photo = new static;
$photo->saveAs($name,$type);
return $photo;
}
public function saveAs($name,$type)
{
$this->path = sprintf("%s/%s/%s-%s",'baseDir',$type,time(), $name);
$this->name = sprintf("%s-%s", time(), $name);
$this->type = $type;
}
public function moves(UploadedFile $file,$type)
{
$file->move($this->baseDir.'/'.$type, $this->name);
return $this;
}
並在您想要添加圖像的模型中,例如Post:
public function images()
{
return $this->hasMany(Image::class);
}
public function addImages(Image $image)
{
return $this->images()->save($image);
}
在控制器中:
public function addImages(ImageRequest $request,$id)
{
$image = $this->makeImage($request->file('file'),$request->type);
Post::where('id' ,'=', $id)->first()->addImage($image);
return back();
}
protected function makeImage(UploadedFile $file,$type)
{
return Image::named($file->getClientOriginalName(),$type)->moves($file,$type);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.