I have attempted to create a multi step form however whenever I try to select add product image https://i.stack.imgur.com/r37Ph.png it returns an error https://i.stack.imgur.com/dLf9f.png. Below I will show the code for my create-step1.blade.php as well as the controller. If anything else is needed to show what is causing the error please let me know.
create-step1.blade.php
<h1 class="text-center">Step 1- Add New Recipe</h1>
<hr>
<form action="/recipes/create-step1" method="post">
{{ csrf_field() }}
<div class="container">
<div class="row">
<div class="col-8 offset-2">
<div class="row">
<h1>Add New Recipe</h1>
</div>
<div class="form-group row">
<label for="title" class="col-md-4 col-form-label">Recipe Title</label>
<input id="title"
type="text"
class="form-control{{ $errors->has('title') ? ' is-invalid' : '' }}"
name="title"
value="{{ old('title') }}"
autocomplete="title" autofocus>
@if ($errors->has('title'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('title') }}</strong>
</span>
@endif
</div>
<hr>
<section class="Description">
<div class="form-group row">
<label for="description" class="col-md-4 col-form-label">Recipe Description</label>
<textarea class="form-control {{ $errors->has('description') ? ' is-invalid' : '' }}"
name="body"
id="description"
rows="5"
placeholder="Your Post Here"
value="{{ old('description') }}"
autocomplete="description" autofocus>
</textarea>
@if ($errors->has('description'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('description') }}</strong>
</span>
@endif
</div>
</section>
<hr>
<button type="submit" class="btn btn-primary">Add Product Image</button>
</div>
</div>
</div>
</form>
@endsection
RecipesController.php
class RecipesController extends Controller
{
/**
* Show the step 1 Form for creating a new product.
*
* @param Request $request
* @return Application|Factory|View
*/
public function createStep1(Request $request)
{
$recipe = $request->session()->get('recipe');
return view('recipes.create-step1',compact('recipe', $recipe));
}
/**
* Post Request to store step1 info in session
*
* @param \Illuminate\Http\Request $request
* @return Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function postCreateStep1(Request $request)
{
$validatedData = $request->validate([
'title' => 'required',
]);
if(empty($request->session()->get('recipe'))){
$recipe = new PostRecipes();
$recipe->fill($validatedData);
$request->session()->put('recipe', $recipe);
}else{
$recipe = $request->session()->get('recipe');
$recipe->fill($validatedData);
$request->session()->put('recipe', $recipe);
}
return redirect('/recipes/create-step2');
}
/**
* Show the step 2 Form for creating a new recipe.
*
* @return Application|Factory|Response|View
*/
public function createStep2(Request $request)
{
$recipe = $request->session()->get('recipe');
return view('recipes.create-step2',compact('recipe', $recipe));
}
/**
* Post Request to store step1 info in session
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function postCreateStep2(Request $request)
{
$recipe = $request->session()->get('recipe');
if(!isset($recipe->productImg)) {
$request->validate([
'recipeimg' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$fileName = "recipeImage-" . time() . '.' . request()->recipeimg->getClientOriginalExtension();
$request->recipeimg->storeAs('recipeimg', $fileName);
$product = $request->session()->get('recipe');
$product->recipeImg = $fileName;
$request->session()->put('recipe', $recipe);
}
return redirect('/recipes/create-step3');
}
/**
* Show the Product Review page
*
* @param Request $request
* @return Application|Factory|View
*/
public function removeImage(Request $request)
{
$recipe = $request->session()->get('recipe');
$recipe->recipeImg = null;
return view('recipes.create-step2',compact('product', $recipe));
}
}