krekas's avatar
Level 25

FlyerProject uploading file problem

So this is my Photo model

<?php

namespace App;

use UploadedFile;
use Illuminate\Database\Eloquent\Model;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class Photo extends Model
{
    protected $table = 'flyer_photos';

    protected $fillable = ['path'];

    protected $baseDir = 'flyers/photos';

    public function flyer()
    {
        return $this->belongsTo('App\Flyer');
    }

    public static function fromForm(UploadedFile $file)
    {
        $photo = new static;

        $name = time() . $file->getClientOriginalName();

        $photo->path = $photo->baseDir . '/' . $name;

        $file->move($photo->baseDir, $name);

        return $photo;
    }
}

And the error I get is

FatalErrorException in Photo.php line 7:
Cannot use Symfony\Component\HttpFoundation\File\UploadedFile as UploadedFile because the name is already in use

But when i write like this

public static function fromForm(Symfony\Component\HttpFoundation\File\UploadedFile\UploadedFile $file)

And try to upload photo then I get a new error

ErrorException in Photo.php line 22:
Argument 1 passed to App\Photo::fromForm() must be an instance of App\Symfony\Component\HttpFoundation\File\UploadedFile\UploadedFile, instance of Symfony\Component\HttpFoundation\File\UploadedFile given, called in C:\xampp\htdocs\project-flyer\app\Http\Controllers\FlyersController.php on line 80 and defined
0 likes
2 replies
uxweb's avatar
uxweb
Best Answer
Level 20

You have the wrong import of UploadedFile at the top.

Use UploadedFile;

1 like
krekas's avatar
Level 25

Don't know why I puted that line there and how i didn't saw it at all.

Please or to participate in this conversation.