You probably missed a step ;) I know Jeffrey is going pretty fast through the series..
Oct 4, 2015
6
Level 1
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;
}
}
Please or to participate in this conversation.