类型错误:传递给App \ AddPhotoToProduct :: __ construct()的参数2必须是App \ Uploaded的实例,

时间:2017-02-06 12:53:54

标签: php symfony file-upload laravel-5 image-uploading

我收到无效参数2的错误,我正试图通过dropzone上传多个图片。

  AddPhotoToProduct.php第30行中的

[ERROR] FatalThrowableError:   类型错误:传递给App \ AddPhotoToProduct :: __ construct()的参数2必须是> App的一个实例,上传,给定的数组,在> C:\ xampp \ htdocs \ mid_login1 \ app \ Http \ Controllers \ ProductPhotoController中调用。 php on>第37行

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     

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}";
}

}

形状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');

1 个答案:

答案 0 :(得分:0)

$ request-&gt;文件是FileBag的一个实例,而不是App / Uploaded的实例,您应该查看Symfony Doc有关HttpFoundation Component的内容,以便更好地了解它的工作原理。

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