callumcarlstrom's avatar

Handle image upload Laravel 5.8

I have an image upload in one of my recipe applications that has been working fine up until this point. It suddenly broke and it will only allow me to post my recipe if I don't upload an image and I let it revert back to the default "noimage.jpg". Could somebody take a look at my ogic and see where I'm going wrong? I don't understand where it is breaking. Thank you.

Controller store function

// Handle File Upload

        if($request->hasfile('recipeImage')){
            // Get filename with extension
            $fileameWithExt = $request->file('recipeImage')->getClientOriginalName();
            // Get just filename
            $filename = pathinfo($fileameWithExt, PATHINFO_FILENAME);
            // Get just extension
            $extension = $request->file('recipeImage')->getClientOriginalExtension();
            // Filename to store
            $fileNameToStore = $filename . '_' . time() . '.' . $extension;
            // Upload Image
            $path = $request->file('recipeImage')->storeAs('public/recipe_images', $fileNameToStore);
        } else {
            $fileNameToStore = 'noimage.jpg';
        }


        $recipe = new Recipe;
            $recipe->author = auth()->user()->username;
            $recipe->title = $request->input('title');
            $recipe->description = $request->input('description');
            $recipe->ingredients = $request->input('ingredients');
            $recipe->directions = $request->input('directions');
            $recipe->recipeImage = $fileNameToStore;
            $recipe->prepTime = $request->input('prepTime');
            $recipe->cookTime = $request->input('cookTime');
            $recipe->servings = $request->input('servings');
            $recipe->calories = $request->input('calories');
            $recipe->user_id = auth()->user()->id;
            $recipe->save();

Blade view with form

<form method="POST" action="{{ action('RecipesController@store') }}" id="submitRecipeForm">
    <div class="photoUploadInput">
                    <label for="imageUpload">
                        <input type="file" id="imageUpload" name="recipeImage">
                    </label>
                    <span>Add a photo of your recipe!</span>
                </div>

Let me know if you need to see anymore code. Thank you

0 likes
5 replies
Nakov's avatar
Nakov
Best Answer
Level 73

On your form element you are missing the enctype. So change it to this:

<form method="POST" action="{{ action('RecipesController@store') }}" id="submitRecipeForm" enctype="multipart/form-data">
callumcarlstrom's avatar

@NAKOV - I added that back in (don't know why I didn't have it there in the first place) but I have the same problem. Still get redirected back without any insert in DB.

charlozard's avatar

Have you tested whether you actually reach the code block you've posted? Maybe there is some file validation you're not passing, just a long shot.

Nakov's avatar

@CALLUMCARLSTROM - Change hasfile to hasFile or just file to make sure that that condition passes, and try printing out in the block there to make sure you are reaching that point at all.

Please or to participate in this conversation.