Please am trying to update my product entry in the database but I get an undefined variable error on submit. The form is supposed to update my database entry but when I route to the update method without passing the product id, I get a missing parameter product error and when add itas is the code below, I get an undefined variable product error
This is my update function
public function update(Request $request, Product $product)
{
$my_array = [$request->file('primary_image'), $request->file('image_1'), $request->file('image_2')];
$insert_array = [];
foreach ($my_array as $item) {
$save_url = '';
if ($item) {
$image = $item;
$name_gen = md5(rand(1000, 10000)) .'.'. $image->getClientOriginalExtension();
Image::make($image)->resize(523,605)->save('upload/products/'.$name_gen);
$save_url = 'upload/products/'.$name_gen;
}
array_push($insert_array, $save_url);
}
$product = Product::find($product->id);
$product->update([
'name' => $request->name,
'category' => $request->category,
'price' => $request->price,
'description' => $request->description,
'status' => $request -> status,
'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' => $insert_array[0],
'image_1' => $insert_array[1],
'image_2' => $insert_array[2],
]);
$notification = array(
'message' => 'Product created successfully',
'alert-type' => 'success'
);
return redirect()->back()->with($notification);
}
this is my blade component
<form method="POST" action="{{route('products.update', ['products' => $product->id])}}" enctype="multipart/form-data">
{{ method_field('PUT') }}
#csrf
<h4 class="card-title">Edit Product</h4><br><br>
<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="category" 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="price" 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="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($product->image):url('upload/no_image.jpg') }}" alt="Product image">
{{-- <img id="showImage" class="" width="300px"src="{{ !empty($product->image) ? url($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 Second Image
</label>
<div class="col-sm-6">
<input name="image_1" accept="image/*" class="form-control"
type="file" id="image" multiple="">
</div>
</div>
<!-- end row -->
<div class="row mb-3">
<label for="example-text-input" class="col-sm-2 col-form-label">Add Third Image
</label>
<div class="col-sm-6">
<input name="image_2" accept="image/*" class="form-control"
type="file" id="image" multiple="">
</div>
</div>
</div>
</div>
<input type="submit" class="btn btn-info waves-effect waves-light" value="Edit Product">
</form>
Please what am I doing wrong?
Thanks for taking the time to review my code
Some refinements to add:
You have product from route model binding at first no need to find it again here: $product = Product::find($product->id);
You are passing the products but your route needs product.
Instead of {{ method_field('PUT') }} use #method('PUT') in your form.
If you had validation you had the data after validation and this code
$product->update([ 'name' => $request->name, 'category' => $request->category, 'price' => $request->price, 'description' => $request->description, 'status' => $request -> status, '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' => $insert_array[0], 'image_1' => $insert_array[1], 'image_2' => $insert_array[2], ]);
would be transferred to
$validatedData = $request->validate([ .... ]); $product->update($validatedData + [ 'primary_image' => $insert_array[0], 'image_1' => $insert_array[1], 'image_2' => $insert_array[2], ]);
or even moving the validation to a separate form request class.
Related
I am getting this kind of error call to member function update on null(). shown my code below please anyone can solve
all are the things are fine when am getting to the image file update in that time am getting this error.
here is my controller code
////
function user_update(Request $request){
$user= User::findOrFail($request->id)->update([
'name'=>$request->name,
'email'=>$request->email,
'address'=>$request->address,
'nationality'=>$request->nationality,
'date_of_birth'=>$request->date_of_birth,
'father_name'=>$request->father_name,
'mother_name'=>$request->mother_name,
'gender'=>$request->gender,
'n_photo'=>$request->n_photo,
]);
if ($request->hasFile('n_photo')) {
$photo_upload = $request ->n_photo;
$photo_extension = $photo_upload -> getClientOriginalExtension();
$photo_name = "toletx_auth_image_". $user . "." . $photo_extension;
Image::make($photo_upload)->resize(452,510)->save(base_path('public/uploads/auth/'.$photo_name),100);
User::find($user)->update([
'n_photo' => $photo_name,
]);
}
return back()->with('success','User information have been successfully Updated.');
}
///////my blade file
<form method="POST" action="{{ route('user_update') }}" enctype="multipart/form-data">
#csrf
<div class="form-group row">
<input type="text" name="id" value="{{$list->id}}">
</div>
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">User name</label>
<div class="col-sm-12 col-md-10">
<input class="form-control" value="{{$list->name}}" name="name" placeholder="Location" type="text" >
</div>
</div>
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">Email</label>
<div class="col-sm-12 col-md-10">
<input class="form-control" value="{{$list->email}}" name="email" placeholder="Location" type="text">
</div>
</div>
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">Mobile Number</label>
<div class="col-sm-12 col-md-10">
<input class="form-control" value="{{$list->phone}}" name="phone" placeholder="Location" type="text" >
</div>
</div>
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">Address</label>
<div class="col-sm-12 col-md-10">
<input class="form-control" value="{{$list->address}}" name="address" placeholder="Location" type="text">
</div>
</div>
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">Nationality</label>
<div class="col-sm-12 col-md-10">
<input class="form-control" name="nationality" value="{{$list->nationality}}" placeholder="nationality" type="numeric">
</div>
</div>
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">Date of birth</label>
<div class="col-sm-12 col-md-10">
<input type="text" class="form-control" placeholder="Birth Date" name="date_of_birth" value="{{$list->date_of_birth}}">
</div>
</div>
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">Father Name</label>
<div class="col-sm-12 col-md-10">
<input type="text" class="form-control" placeholder="Father name" name="father_name" value="{{$list->father_name}}">
</div>
</div>
<div class="form-group row">
<label class="col-sm-12 col-md-2 col-form-label">Mother Name</label>
<div class="col-sm-12 col-md-10">
<input type="text" class="form-control" placeholder="mother name" name="mother_name" value="{{$list->mother_name}}">
</div>
</div>
<div class="form-group">
<div class="form-group form-float">
<div class="card">
<div class="body">
<input type="file" class="dropify" name="n_photo" >
<img src="{{ asset('uploads/auth') }}/{{ $list->n_photo }}" alt="">
</div>
</div>
</div>
<button class="btn btn-primary" type="submit">Update</button>
You've got 2 main problems with this code:
User::find($user)->update(['n_photo' => $photo_name]);
First of all, the correct syntax would be to use $user->id, not $user:
User::find($user->id)->update(['n_photo' => $photo_name]);
Second, that is super redundant; $user is already the result of User::find(), so you're doubling up for no reason.
To fix your issue, simply adjust your code to:
$user->update(['n_photo' => $photo_name]);
Just spacing issue please check these two lines
//Before // Wrong
$photo_upload = $request ->n_photo;
$photo_extension = $photo_upload -> getClientOriginalExtension();
//After //Correct
$photo_upload = $request->n_photo;
$photo_extension = $photo_upload->getClientOriginalExtension();
function user_update(Request $request){
$photo_name = '';
$user= User::findOrFail($request->id);
if ($request->hasFile('n_photo')) {
$photo_upload = $request->n_photo;
$photo_extension = $photo_upload->getClientOriginalExtension();
$photo_name = "toletx_auth_image_". $user . "." . $photo_extension
Image::make($photo_upload)->resize(452,510)->save(base_path('public/uploads/auth/'.$photo_name),100);
}
$user->update([
'name'=>$request->name,
'email'=>$request->email,
'address'=>$request->address,
'nationality'=>$request->nationality,
'date_of_birth'=>$request->date_of_birth,
'father_name'=>$request->father_name,
'mother_name'=>$request->mother_name,
'gender'=>$request->gender,
'n_photo'=>$photo_name,
]);
}
return back()->with('success','User information have been successfully Updated.');
}
I created a modal using Bootstrap, to add a new row into table.
I don't know why and how doesn't work.
This is table_edit.blade.php MODAL:
<div id="formModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Adaugare Rolete</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<span id="form_result"></span>
<form method="post" id="sample_form" class="form-horizontal" action ="{{route('tabledit.store')}}">
#csrf
<div class="form-group">
<label class="control-label col-md-4" >ProdusID : </label>
<div class="col-md">
<input type="text" name="ProdusID" id="ProdusID" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4">Denumire : </label>
<div class="col-md">
<input type="text" name="Denumire" id="Denumire" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" >Cant1 : </label>
<div class="col-md">
<input type="text" name="Cant1" id="Cant1" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" >Cant2 : </label>
<div class="col-md">
<input type="text" name="Cant2" id="Cant2" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" >Cant3 : </label>
<div class="col-md">
<input type="text" name="Cant3" id="Cant3" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" >Cant4 : </label>
<div class="col-md">
<input type="text" name="Cant4" id="Cant4" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" >Data : </label>
<div class="col-md">
<input type="date" name="Data" id="Data" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" >Pret Intrare : </label>
<div class="col-md">
<input type="text" name="Pret_Intrare" id="Pret_Intrare" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" >Pret Iesire : </label>
<div class="col-md">
<input type="text" name="Pret_Iesire" id="Pret_Iesire" class="form-control" />
</div>
</div>
<br />
<div class="form-group" align="center">
<input type="hidden" name="action" id="action" value="Add" />
<input type="hidden" name="hidden_id" id="hidden_id" />
<input type="submit" name="action_button" id="action_button" class="btn btn-warning" value="Add" />
</div>
</form>
</div>
</div>
</div>
</div>
Here we can see I do an easy html bootstrap, it's oke, it's work, but affter, maybe in Controller it's the problem.
This is Controller:
public function store(Request $request){
$rules = array(
'ProdusID' => 'required',
'Denumire' => 'required',
'Cant1' => 'required',
'Cant2' => 'required',
'Cant3' => 'required',
'Cant4' => 'required',
'Data' => 'required',
'Pret_Intrare' => 'required',
'Pret_Iesire' => 'required'
);
$error = Validator::make($request->all(), $rules);
if($error->fails()){
return response()->json(['errors' => $error->errors()->all()]);
}
$form_data = array(
'ProdusID' => $request->ProdusID,
'Denumire' => $request->Denumire,
'Cant1' => $request->Cant1,
'Cant2' => $request->Cant2,
'Cant3' => $request->Cant3,
'Cant4' => $request->Cant4,
'Data' => $request->Data,
'Pret_Intrare' => $request->Pret_Intrare,
'Pret_Iesire' => $request->Pret_Iesire
);
Rolete::create($form_data);
return response()->json(['succes' => 'Data Added successfully.']);
}
And web:
Route::post('tabledit/store', 'TableditControllerRolete#store')->name('tabledit.store');
This is an image with modal
(!) UPDATE: I don't have any error, but he doesn't adding into table.
your form don't have an action attribute. you should fix that line with this:
<form method="post" id="sample_form" class="form-horizontal" action ="{{route('tabledit.store'}}">
I have a problem, I cannot update the user profile, when I press "submit" I will get an expired page, how can I solve it?
My form:
<form method="post" class ="tab-pane" id ="edit" action="{{route('profile')}}">
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Email</label>
<div class="col-lg-9">
<input class="form-control" type="email" value={{$user->email}}>
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">About</label>
<div class="col-lg-9">
<input class="form-control" type="text" value={{$user->about}}>
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Skills</label>
<div class="col-lg-9">
<input class="form-control" type="text" value={{$user->skills}}>
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Hobbies</label>
<div class="col-lg-9">
<input class="form-control" type="text" value={{$user->hobbies}}>
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Address</label>
<div class="col-lg-9">
<input class="form-control" type="text" value="" placeholder="Street">
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label"></label>
<div class="col-lg-6">
<input class="form-control" type="text" value="" placeholder="City">
</div>
<div class="col-lg-3">
<input class="form-control" type="text" value="" placeholder="State">
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Username</label>
<div class="col-lg-9">
<input class="form-control" type="text" value={{$user->name}}>
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Password</label>
<div class="col-lg-9">
<input class="form-control" type="password" value={{$user->password}}>
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label"></label>
<div class="col-lg-9">
<input type="reset" class="btn btn-secondary" value="Cancel">
<<button type="submit">submit</button>
</div>
</div>
</form>
</div>
</div>
My route:
Route::post('/profile', 'UserController#update_Profile');
And my UserController:
public function update_Profile(User $user) {
$this->validate(request(), [
'name' => 'required',
'email' => 'required|email|unique:users',
]);
$user->name = Request::input('name');
$user->email = Request::input('email');
$user->save();
Flash::message('Your account has been updated!');
return back();
}
I tried different methods through topics but failed, what do you think is the problem? Maybe because of how I defined view or because of the route?
Put #CSRF in your form.
Like so:
<form method="post" class ="tab-pane" id ="edit" action="{{route('profile')}}">
#csrf
<div class="form-group row">
<label class="col-lg-3 col-form-label form-control-label">Email</label>
<div class="col-lg-9">
<input class="form-control" type="email" value={{$user->email}}>
</div>
</div>
...
...
</form>
See this: https://laravel.com/docs/5.8/csrf
I'm working on a page for editing user profiles but I want the page am working on to submit to itself when it is submitted and show a message that the profile has been edited successfully. Please how do I do this ?
Here is what am working ?
<div class="row">
<div class="text-center title">Pricing</div>
<div class="text-center desc col-md-8 col-md-push-2">
{{$sitename}}
</div>
<div class="container" style="padding-top: 60px;">
<h1 class="page-header">Edit Profile</h1>
<div class="row">
<!-- left column -->
<form class="form-horizontal" role="form" method="post" action="/profile">
<div class="col-md-4 col-sm-6 col-xs-12">
<div class="text-center">
<img id="ShowImage" src="#"/>
<img src="http://localhost:8234/img/index.png" class="avatar img-circle img-thumbnail" alt="avatar" width="200" height="200">
<h6>Upload a different photo...</h6>
<input type="file" class="text-center center-block well well-sm" name="avatar_path" id="avatar_path" onchange="readURL(this);">
</div>
</div>
<!-- edit form column -->
<div class="col-md-8 col-sm-6 col-xs-12 personal-info">
<div class="alert alert-info alert-dismissable">
<a class="panel-close close" data-dismiss="alert">×</a>
<i class="fa fa-coffee"></i>
This is the <strong>Profile Page</strong>. Use this to <strong>ONLY</strong> change your peronsal details
</div>
<h3>Personal info</h3>
<input class="form-control" value="{{$userInfo['data']['id']}}" type="hidden" name="user_id">
<div class="form-group">
<label class="col-lg-3 control-label">First Name:</label>
<div class="col-lg-8">
<input class="form-control" value="{{$userInfo['data']['first_name']}}" type="text" name="first_name">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Last Name:</label>
<div class="col-lg-8">
<input class="form-control" value="{{$userInfo['data']['last_name']}}" type="text" name="last_name">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Username:</label>
<div class="col-lg-8">
<input class="form-control" value="{{$userInfo['data']['profile']['username']}}" type="text" name="username">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Email Address:</label>
<div class="col-lg-8">
<input class="form-control" value="{{$userInfo['data']['email']}}" type="text" name="email">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Gender</label>
<div class="col-lg-8">
<div class="ui-select">
<select id="gender" class="form-control" name="gender">
<option value="{{$userInfo['data']['profile']['gender']}}" selected>{{$userInfo['data']['profile']['gender']}}</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">City:</label>
<div class="col-lg-8">
<input class="form-control" value="{{$userInfo['data']['profile']['city']}}" type="text" name="city">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">State:</label>
<div class="col-lg-8">
<input class="form-control" value="{{$userInfo['data']['profile']['state']}}" type="text" name="state">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Country:</label>
<div class="col-lg-8">
<input class="form-control" value="{{$userInfo['data']['profile']['country']}}" type="text" name="country">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Mobile:</label>
<div class="col-lg-8">
<input class="form-control" value="{{$userInfo['data']['profile']['mobile']}}" type="text" name="mobile">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Occupation:</label>
<div class="col-lg-8">
<input class="form-control" value="{{$userInfo['data']['profile']['occupation']}}" type="text" name="occupation">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="bkgrnd-blue text-white btn btn-primary" value="Update Profile" type="submit">
<span></span>
Cancel
</div>
</div>
</form>
</div>
</div>
</div>
This solution came from https://laravel.io/forum/01-30-2015-form-submission-to-the-same-page. Hope it helps
I have done a get route to display the page. I then did route a post to post form data.
Then I passed the $data variable to blade where I did an isset to check if it is created which displays the results
Display initial page
public function destinationSearchGet(){
$headData = array('pageTitle' => 'Admin Home - View all destinations');
return view('admin.destination_search', $headData);
}
post data back to the same page and create a new variable
public function destinationSearchPost(){
$headData = array('pageTitle' => 'Admin Home - Search results');
$formData = Request::input('destination');
$data = ParentRegionList::destinationSearch($formData);
return view('admin.destination_search', $headData)->with(compact('data'))
}
use blade to check if it exists
#if (isset($data))
<p>{{dd($data)}}</p>
#endif
I have a form in my Laravel 4 web app that is refusing to submit to the database. Each time i try to submit, the page just reloads and i see no error messages even in the laravel log. I have spent two days trying to figure out what the problem is as I can't seem to see anything wrong with the code.
Any help will be appreciated.
/**** THE FORM VIEW ***/
<div class="container">
<div class="row">
#if(Session::has('success'))
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
{{Session::get('success')}}
</div>
#elseif(Session::has('fail'))
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
{{Session::get('fail')}}
</div>
#endif
</div>
#include('partials.admin-navbar')
<div class="admin_profile_content">
<form class="" method="post" action=" {{URL::route('postSubmitCompetition')}}">
<div class="" style="width:80%; margin:auto;">
<div class="panel panel-default">
<div class="panel-heading">Organization </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label">Select the Organization hosting this competition </label>
<div class="col-sm-10">
{{ Form::select('organization', $organizations, null, ['class' => 'form-control']) }}
<p class="text-danger">
#if($errors->has('organization'))
{{ $errors->first('organization') }}
#endif
</p>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Competition name </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label">Competition name </label>
<div class="col-sm-10">
<input type="text" class="form-control" id="" name="competition_name">
<p class="text-danger">
#if($errors->has('competition_name'))
{{ $errors->first('competition_name') }}
#endif
</p>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Prizes </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label">Total prize pool</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="" name="total_prize">
</div>
</div>
<div class="form-group" style="padding-top:41px;">
<label for="" class="col-sm-2 control-label">Number of prize-winning places</label>
<div class="col-sm-1">
Top
</div>
<div class="col-sm-2">
<input type="text" class="form-control" id="" name="number_of_winning_places">
<p class="text-danger">
#if($errors->has('number_of_winning_places'))
{{ $errors->first('number_of_winning_places') }}
#endif
</p>
</div>
<div class="col-sm-3">
competitors will win a prize
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Timeline </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label"> Start and end date </label>
<div class="col-sm-3">
<input type="text" class="form-control" id="competition_start_date" name="competition_start_date">
<p class="text-danger">
#if($errors->has('competition_start_date'))
{{ $errors->first('competition_start_date') }}
#endif
</p>
<input type="hidden" name="hidden_start_date" id="hidden_start_date">
</div>
<div class="col-sm-2">
to
</div>
<div class="col-sm-3">
<input type="text" class="form-control" id="competition_end_date" name="competition_end_date">
<p class="text-danger">
#if($errors->has('competition_end_date'))
{{ $errors->first('competition_end_date') }}
#endif
</p>
<input type="hidden" name="hidden_end_date" id="hidden_end_date">
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Competition details </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label"> Competition details </label>
<div class="col-sm-10">
<textarea class="form-control" name="competition_details" id="competition_details" rows="10"> </textarea>
<p class="text-danger">
#if($errors->has('competition_details'))
{{ $errors->first('competition_details') }}
#endif
</p>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Competition Status </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label"> Set competition status </label>
<div class="col-sm-10">
<select class="form-control" name="competition_status" id="competition_status">
<option value="0">Coming Soon</option>
<option value="1">Live </option>
</select>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Competition data </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label"> Upload the data for the competition </label>
<div class="col-sm-10">
<input type="file" id="competition_data_1" name="competition_data_1">
<p class="help-block"> Data file/folder 1</p>
</div>
</div>
</div>
</div>
{{ Form::token() }}
<div class="panel panel-default">
<div class="panel-heading">Upload Competition </div>
<div class="panel-body">
<div class="form-group">
<div class="col-sm-10">
<input type="submit" class="btn btn-primary btn-lg btn-block" id="submit_competition" value="Submit">
</div>
</div>
</div>
</div>
</div>
</form>
<hr>
/* the controller method */
public function postSubmitCompetition()
{
$validator = Validator::make(Input::all(), array(
'organization' => 'required',
'competition_name' => 'required',
'prize_winning_places' => 'required',
'competition_start_date' => 'required',
'competition_end_date' => 'required',
'competition_details' => 'required',
'status' => 'required'
));
if($validator->fails())
{
return Redirect::route('getSubmitCompetition')->withErrors($validator)->withInput();
}
else
{
$competition = new Competition();
$competition->hosting_organization_id = Input::get('organization');
$competition->competition_name = Input::get('competition_name');
$competition->total_prize_pool = Input::get('total_prize');
$competition->prize_winning_places = Input::get('number_of_winning_places');
$competition->start_date = Input::get('competition_start_date');
$competition->end_date = Input::get('competition_end_date');
$competition->competition_details = Input::get('competition_details');
$competition->status = Input::get('competition_status');
if($competition->save())
{
return Redirect::route('getSubmitCompetition')->with('success', 'You have successfully created this competition');
}
else
{
return Redirect::route('getSubmitCompetition')->with('fail', 'An error occurred while creating that competition. Please contact sys admin');
}
}
}
/* the route */
Route::post('/admin/submit-a-competition', array('uses' => 'CompetitionController#postSubmitCompetition', 'as' => 'postSubmitCompetition'));
Problem
Laravel never saves your data because the validator fails.
$validator = Validator::make(Input::all(), array(
'organization' => 'required',
'competition_name' => 'required',
'prize_winning_places' => 'required',
'competition_start_date' => 'required',
'competition_end_date' => 'required',
'competition_details' => 'required',
'status' => 'required'
));
You require prize_winning_places and status but there are no input fields for these two in the view.
The missing input fields:
Field name: status - Problem: You made a select with the name: competition_status instead of: status
<select class="form-control" name="competition_status" id="competition_status">
<option value="0">Coming Soon</option>
<option value="1">Live </option>
</select>
Field name: prize_winning_places - Problem: You made an input field with the name: ** number_of_winning_places **instead of prize_winning_places
<input type="text" class="form-control" id="" name="number_of_winning_places">
Solution
Change the $validator variable to:
$validator = Validator::make(Input::all(), array(
'organization' => 'required',
'competition_name' => 'required',
'number_of_winning_places' => 'required',
'competition_start_date' => 'required',
'competition_end_date' => 'required',
'competition_details' => 'required',
'competition_status' => 'required'
));
Check this line:
<form class="" method="post" action=" {{URL::route('postSubmitCompetition')}}">
here form action is the route name not the function name. Like:
Route::post('/save_data', array('uses' => 'CompetitionController#postSubmitCompetition', 'as' => 'postSubmitCompetition'));
It will route your route to the function.