jeudi 5 décembre 2019

Dropzone "Call to a member function store() on string" with Laravel

I want to store data which includes images but I'm getting an error "Call to a member function store() on string" when I try to pass the image to the database. On console(request) it looks like photos[]: IMG_1207.jpg. How can I pass the image to the database?

Controller

   public function store(Request $request)
{
$formInput=$request->except('filename');

$product = product::create(array_merge($formInput, [
    'user_id' => Auth::user()->id
]));
foreach ($request->photos as $photo) {
    $filename = $photo->store('public/photos');
    ProductsPhoto::create([
        'product_id' => $product->id,
        'filename' => $filename
    ]);
 }
}

Script

  var uploadedDocumentMap = {}
Dropzone.options.documentDropzone = {
    url: '',
    maxFilesize: 10, // MB
    addRemoveLinks: true,
    headers: {
        'X-CSRF-TOKEN': ''
    },
    success: function (file, response) {
        console.log(file);
        console.log(response);
        $('form').append('<input type="hidden" name="photos[]" value="' + file.name + '">')
    },
    removedfile: function (file) {
        file.previewElement.remove()
        var name = ''
        if (typeof file.file_name !== 'undefined') {
            name = file.file_name
        } else {
            name = uploadedDocumentMap[file.name]
        }
        $('form').find('input[name="photos[]"][value="' + name + '"]').remove()
    },
    init: function () {
    }
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire