I have a laravel form to create a new product entry in a database on submit, am supposed to be redirected back but I get a completely blank page without any errors. There is no new entry in my database when I check. The form is made up of various text fields, an image URL, and a multi-selected Image URL
please this is my blade template
<form method="POST" action="{{ route('products.store') }}" enctype="multipart/form-data">
<h4 class="card-title">Create Product</h4><br><br>
@csrf
<div class="row">
<div class="col">
<div class="row mbr-1">
<label for="example-text-input" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-6">
<input name="name" class="form-control" type="text" value=""
id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input"
class="col-sm-2 col-form-label">Category</label>
<div class="col-sm-6">
<input name="name" class="form-control" type="text" value=""
id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Price</label>
<div class="col-sm-6">
<input name="catchy_title" class="form-control" type="number"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Status</label>
<div class="col-sm-6">
<input name="status" class="form-control" type="text" value=""
id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Product
Description</label>
<div class="col-sm-8">
<textarea id="elm1" name="long_description" placeholder="Please enter a vivid description of the product"></textarea>
</div>
</div>
<!-- end row -->
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Tags</label>
<div class="col-sm-6">
<input name="tags" class="form-control" type="text" value=""
id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Estimated
Delivery
Time</label>
<div class="col-sm-6">
<input name="estimated_delivery_time" class="form-control" type="text"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Available
Quantity</label>
<div class="col-sm-6">
<input name="available_quantity" class="form-control" type="text"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Colors</label>
<div class="col-sm-6">
<input name="colors" class="form-control" type="text" value=""
id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Supplier's
Name</label>
<div class="col-sm-6">
<input name="supplier_name" class="form-control" type="text"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Supplier's
Contact</label>
<div class="col-sm-6">
<input name="supplier_contact" class="form-control" type="text"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Video Url /
Link</label>
<div class="col-sm-6">
<input name="video_description" class="form-control" type="text"
value="" id="example-text-input">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Primarry
Image</label>
<div class="col-sm-6">
<input name="primary_image" accept="image/*" class="form-control"
type="file" id="image">
</div>
</div>
<!-- end row -->
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label"> Primary Image
Preview</label>
<div class="col-sm-6">
<img id="showImage" class="" width="300px"
src="{{ !empty($Product->image) ? url('upload/products/' . $product->image) : url('upload/no_image.jpg') }}"
alt="Hero image">
</div>
</div>
<!-- end row -->
</div>
<div class="col">
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Add Other
Images</label>
<div class="col-sm-6">
<input name="multi_image[]" accept="image/*" class="form-control"
type="file" id="image" multiple="">
</div>
</div>
<!-- end row -->
</div>
</div>
<input type="submit" class="btn btn-info waves-effect waves-light"
value="Create Product">
</form>
and this is my store function
public function store(Request $request)
{
if ($request->file('image')) {
$image = $request->file('image');
$name_gen = hexdec(uniqid()).'.'.$image->getClientOriginalExtension(); // 3434343443.jpg
Image::make($image)->resize(523,605)->save('upload/home_about/'.$name_gen);
$save_url = 'upload/products/'.$name_gen;
$this->validate($request, [
'name' => $request->name,
'category' => $request->category,
'price' => $request->price,
'description' => $request->description,
'status' => $request -> status,
'tags' => $request -> tags,
'estimated_delivery_time' => $request->estimated_delivery_time,
'available_quantity' => $request->available_quantity,
'colors' => $request->colors,
'supplier_name' => $request->supplier_name,
'supplier_phone' => $request->supplier_phone,
'video_description' => $request->video_description,
'primary_image' => $save_url,
'other_images' => $save_url,
]);
$notification = array(
'message' => 'Product created successfully',
'alert-type' => 'success'
);
return redirect()->back()->with($notification);
}
Please what am I doing wrong?
thank you for taking some time to review
3
Answers
Try to be specific, pass route where you want to redirect like below:
you are validating the request but not saving it to database.
after the validation and before the return you should do like this to save in database.
In the snippet above you’re performing the validation, but I don’t see a request to persist the data to your database in your
store()
function.It’s recommended to utilize the functionality provided by the framework when checking for request input presence. These functions usually provide quality-of-life aspects such as checking for multiple input presence and conditional callbacks as well.
https://laravel.com/docs/9.x/requests#determining-if-input-is-present
You should also be calling the
validate()
method on$request
, not$this
.First, I’d like to recommend that you utilize a Form Request
https://laravel.com/docs/9.x/validation#form-request-validation
Second, use the validated data and mass assignment when storing the new model. If the validated data structure is not 1:1 with the table schema, you could always assign the values manually but this will result in a bloated controller. One possible solution is to utilize a DTO (Data Transfer Object) and provide the
validated()
array to the DTO when using mass assignment.Note that if the incoming request fields do not pass the given validation rules Laravel will automatically redirect the user back to their previous location. In addition, all of the validation errors and request input will automatically be flashed to the session.