Type error: Argument 2 passed to App\\AddPhotoToPr

2019-09-17 08:35发布

问题:

I am getting error of invalid argument 2 and I am trying to upload multiple images through dropzone.

[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

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

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

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

Routes File code

 Route::post('/admin/products/{id}/photo','ProductPhotoController@store');

回答1:

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