I'm following the dropzone video (https://laracasts.com/series/build-project-flyer-with-me/episodes/11). In my case a user has many files.
But I'm stuck at the following exception:
Argument 1 passed to App\User::addFile() must be an instance of App\files, instance of Symfony\Component\HttpFoundation\File\UploadedFile given, called in D:\wamp\www\local\app\Http\Controllers\UploadCenterController.php on line 28 and defined
My storeFiles controller:
public function storeFiles(Request $request)
{
$this->validate($request, [
'file' => 'required|mimes:jpg,jpeg,png,bmp'
]);
$file = $request->file('file');
$name = time() . $file->getClientOriginalName();
$file->move('files/user',$name);
$saveFile = files::fromForm($file);
$user = Auth::user()->addFile($saveFile);
}
static method fromForm in files model:
public static function fromForm(UploadedFile $file)
{
$newFile = new static;
$name = time() . $file->getClientOriginalName();
$newFile->path = '/files/user/'.$name;
return $file;
}
And addFile in User model:
public function addFile(files $file)
{
return $this->files()->save($file);
}
Some help please!
--EDIT--
Entire file class:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class files extends Model
{
protected $table = 'user_images';
protected $fillable = ["user_id","path"];
public function user()
{
return $this->belongsTo('App\User');
}
public static function fromForm(UploadedFile $file)
{
$newFile = new static;
$name = time() . $file->getClientOriginalName();
$newFile->path = '/files/user/'.$name;
return $file;
}
}
Found it!
Had to return in my file class (fromForm method)
$newFile instead of $file!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With