clkline's avatar

Project Flyer: $request->file('cover') returns null after refactor

I am working on the Project Flyer, the Bulk Uploads Episode. I got through most of it just fine. I have a few things renamed because I am not simply building exactly what he is building, but using his concepts to kick start my own project.

That being said, the file upload worked fine until the section on refactoring. Now I am getting an error of:

Argument 1 passed to App\StoryCover::fromForm() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, null given, called in /home/vagrant/PersonalProjects/PersonalLibrary/app/Http/Controllers/StoriesController.php on line 71 and defined
dd($request->file('cover'));

does indeed return NULL.

My Code: show.blade.php

@section('scripts.footer')
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.0.1/dropzone.js"></script>
    <script>
        Dropzone.options.addPhotosForm =
        {
            paranName: 'cover',
            maxFileSize: 3,
            acceptedFiles: '.jpg, .jpeg, .png'
        }
    </script>
@stop

StoriesController.php

public function addCover($title, Request $request)
    {
        $this->validate($request, [
            'cover' => 'require|mimes:jpg,jpeg,png'
        ]);

        /*
         * Create the photo object that will be stored
         */
        $photo = StoryCover::fromForm($request->file('cover'));

        /*
         * Store the link to the database
         */
        Story::named($title)->addCover($photo);

        return "Done";
    }

StoryCover.php

?php

namespace App;

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

class StoryCover extends Model
{
    protected $table = 'story_covers';
    protected $basePath = 'stories/covers';

    protected $fillable = ['path'];

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

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

        /*
         * Make sure the file name is unique so files
         * don't get accidently overridden
         */
        $name = time().$file->getClientOriginalName();

        /*
         * Set the save place
         */
        $cover->path = $cover->basePath . "/" . $name;

        $cover->move($cover->basePath, $name);

        return $cover;
    }
}
0 likes
6 replies
bobbybouwmann's avatar

You probably missed a step ;) I know Jeffrey is going pretty fast through the series..

clkline's avatar

He goes back over his refactoring work before he tries anything, and I paused it at every step. My code is verbatim for his, minus what I changed to make it work for my system. I will attempt to look it back over a fourth time from the beginning of the video - but what gets me is that all of this worked before his refactoring.

clkline's avatar
array:2 [
  "_token" => "FJce9PyfEO0OHvtsrvMWclOsR62G9EqMkQ4B9Fb2"
  "file" => UploadedFile {#30
    -test: false
    -originalName: "WP_20151005_08_42_34_Pro.jpg"
    -mimeType: "image/jpeg"
    -size: 533038
    -error: 0
  }
]

So it is called "file", even though I set the paranName to "cover"?

<form id="addPhotosForm" action="/{{ $story->title }}/covers" method="POST" class="dropzone">
    {{ csrf_field() }}
</form>
@section('scripts.footer')
    <script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.0.1/dropzone.js"></script>
    <script>
        Dropzone.options.addPhotosForm =
        {
            paranName: 'cover',
            maxFileSize: 3,
            acceptedFiles: '.jpg, .jpeg, .png'
        }
    </script>
@stop
clkline's avatar

That was it (among a few other fixes that needed to be done). Thank you!

Please or to participate in this conversation.