Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel instance error

Tags:

php

laravel

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;
    }
}
like image 976
Jamie Avatar asked Feb 11 '26 02:02

Jamie


1 Answers

Found it!

Had to return in my file class (fromForm method)

$newFile instead of $file!

like image 106
Jamie Avatar answered Feb 13 '26 17:02

Jamie