键入错误:传递给App \ AddPhotoToProduct :: __ construct()的参数2必须是App \ Uploaded的实例, [英] Type error: Argument 2 passed to App\AddPhotoToProduct::__construct() must be an instance of App\Uploaded,
问题描述
我收到无效参数2的错误,并且尝试通过dropzone上传多张图片.
I am getting error of invalid argument 2 and I am trying to upload multiple images through dropzone.
AddPhotoToProduct.php第30行中的[ERROR] FatalThrowableError: 类型错误:传递给App \ AddPhotoToProduct :: __ construct()的参数2必须是App \ Uploaded的实例,给定的数组在> C:\ xampp \ htdocs \ mid_login1 \ app \ Http \ Controllers \ ProductPhotoController.php中调用>第37行
[ERROR]FatalThrowableError in AddPhotoToProduct.php line 30: Type error: Argument 2 passed to App\AddPhotoToProduct::__construct() must be >an instance of App\Uploaded, array given, called in >C:\xampp\htdocs\mid_login1\app\Http\Controllers\ProductPhotoController.php on >line 37
ProductPhotoController.php
ProductPhotoController.php
namespace App\Http\Controllers;
use App\Product;
use App\ProductPhoto;
use App\AddPhotoToProduct;
use Illuminate\Http\Request;
Use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
class ProductPhotoController extends Controller
{
/**
* @param $id
*
*/
public function store($id,Request $request)
{
$product = Product::LocatedAt($id);
$file = $request->file('file');
(new AddPhotoToProduct($product,$file))->save();
}
}
我的模特 AddPhotoToProduct.php
And My Model AddPhotoToProduct.php
namespace App;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class AddPhotoToProduct {
/**
* @var Product
*/
protected $product;
/**
* The UploadedFile Instance.
*
* @var UploadedFile
*/
protected $file;
/**
* Create a new AddPhotoToProduct form object.
*
* @param Product $product
* @param UploadedFile $file
* @param Thumbnail|null $thumbnail
*/
public function __construct(Product $product, Uploaded $file, Thumbnail $thumbnail = null) {
$this->product = $product;
$this->file =getName($file);
$this->thumbnail = $thumbnail ?: new Thumbnail;
}
/**
* Process the form.
*/
public function save() {
// Attach the photo to the product.
$photo = $this->product->addPhoto($this->makePhoto());
// move a file to the base directory with the file name.
$this->file->move($photo->baseDir(), $photo->name);
// Generate a photo thumbnail.
$this->thumbnail->make($photo->path, $photo->thumbnail_path);
}
/**
* Make a new Photo Instance.
*
* @return ProductPhoto
*/
protected function makePhoto() {
return new ProductPhoto(['name' => $this->makeFilename()]);
}
/**
* Make a Filename, based on the uploaded file.
*
* @return string
*/
protected function makeFilename() {
// Get the file name original name
// and encrypt it with sha1
$name = sha1 (
time() . $this->file->getClientOriginalName()
);
// Get the extension of the photo.
$extension = $this->file->getClientOriginalExtension();
// Then set name = merge those together.
return "{$name}.{$extension}";
}
}
form-uploads.blade.php
form-uploads.blade.php
<div class="row">
<div class="col-md-12 portlets">
<!-- Your awesome content goes here -->
<div class="m-b-30">
<form method="post" action="/admin/products/{{ $products->id }}/photo"
class="dropzone" id="addProductImages">
{{ csrf_field()}}
</form>
</div>
</div>
</div>
路由文件代码
Route::post('/admin/products/{id}/photo','ProductPhotoController@store');
推荐答案
$ request-> file是FileBag的一个实例,而不是App/Uploaded的一个实例,您应该查看Symfony Doc关于
$request->file is an instance of FileBag and not an instance of App/Uploaded you should have a look to Symfony Doc about the HttpFoundation Component to better understand how it works.
class ProductPhotoController extends Controller
{
/**
* @param $id
*
*/
public function store($id,Request $request)
{
$product = Product::LocatedAt($id);
$files = $request->file('file');
foreach ($files as $file) {
(new AddPhotoToProduct($product,$file))->save();
}
}
}
这篇关于键入错误:传递给App \ AddPhotoToProduct :: __ construct()的参数2必须是App \ Uploaded的实例,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!