Why is my data not being submitted into the database - php

I tried to create adding/creating a new menu and saved it in the database. However, when I clicked the button, my system didn't show any error but the data is not saved in the database.
adminAddMenu.blade.php
<form>
#csrf
<div class="form-group row">
<label for="categorycode" class="col-sm-3 col-form-label">Category Code</label>
<div class="col-md-4">
<input name="category_code" value="{{old('category_code')}}" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="menutitle" class="col-sm-3 col-form-label">Menu Title</label>
<div class="col-md-4">
<input name="menu_title" value="{{old('menu_title')}}" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="menuprice" class="col-sm-3 col-form-label">Menu Price</label>
<div class="col-md-4">
<textarea name = "menu_price" value="{{old('menu_price')}}" class="form-control"></textarea>
</div>
</div>
<div class="form-group row">
<div class="offset-sm-3 col-sm-9">
<button type="submit" class="btn btn-primary" href="">Submit</button>
</div>
</div>
</div>
</form>
AdminMenuController.php
public function store(Request $request)
{
$menu = new \App\Menu;
$menu->category_code = $request->category_code;
$menu->menu_title = $request->menu_title;
$menu->menu_price = $request->menu_price;
$menu->save();
Session::flash('flash_message', 'Menu is successful! added');
return redirect()->back();
}
web.php
Route::resource('/menus', 'AdminMenuController');

You don't give the form action method where your data will submit. Like you want to send your form data to store method in your controller. So you have to write follow:
<form method="POST" action="{{ route('your route name') }}">

I think you miss the action attribute of your form. Please add action to your form
<form action="{{ YOUR_ROUTER }}">
...

Please add action on form tag.
Like as-
<form action={{ route("your route write here") }} method="post">
I think now it's work's.

Related

From validation Throws error The GET method is not supported for this route. Supported methods: POST."

i am new to laravel..Kind of stuck at this place. Tried many solutions for this but none worked yet, There are similar question but most unresolved, or proper evident solution not posted yet(from google,stackoverflow ..etc)
i have defned a custom route
Route::post('/ComplaintGenerate', 'ComplaintsController#generate');
whenever i submit the view with 'POST' method as
<form action="/ComplaintGenerate" method="POST" >
without any validation rule in my Complaintscontroller everything works fine and i can save data. but when i put validation either through Requests or direct it throws error Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
if i remove validation everything works fine. I also tried with GET method but still dint work.
A little peace of advice will be very much appreciated.
Web.route
Route::middleware(['auth'])->group(function(){
Route::post('/Complaint', 'ComplaintsController#find');
Route::post('/ComplaintGenerate', 'ComplaintsController#generate');
Route::post('/Complaint/{Complaint}', 'ComplaintsController#save_customer');
Route::resource('Complaints', 'ComplaintsController');
Route::resource('Occupancies', 'OccupanciesController');
Route::resource('Customers', 'CustomersController');
Route::resource('Services', 'ServiceController');
Route::resource('ServiceTeams', 'ServiceTeamController');
Route::get('/home', 'HomeController#index')->name('home');});
My controller:
public function generate(GenerateInitialComplaintRequest $request)
{
$complaint = Complaint::find($request->complaint_id);
$complaint->update([
'complaint_date'=>$request->complaint_date,
'complaint_description'=>$request->complaint_description,
]);
return redirect(route('Complaints.index')->with('complaint', Complaint::all()));
}
my View:
<div class="container my-5">
<div class="col d-flex justify-content-center my-4">
<div class="card">
<div class="card-header">
<form action="/ComplaintGenerate" method="POST" >
#csrf
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<div class="form-row">
<div class="form-group col-md-6">
<label for="complaint_id">Complaint Number</label>
<input type="text" class="form-control" id="complaint_id" name="complaint_id" value="{{$complaint->id}}" readonly >
</div>
<div class="form-group col-md-6">
<label for="complaint_date">Complaint Date</label>
<input type="text" class="form-control" id="complaint_date" name="complaint_date">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="complaint_description">Complaint Description</label>
<textarea class="form-control" id="complaint_description" name="complaint_description" rows="5"></textarea>
</div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
What is the route for displaying your form? When validation fails, Laravel makes redirection using GET method to the route it was displayed from.
I assume the form might be displayed in the find method of your ComplaintsController, and when validation fails, there's redirection to this route and that is what throws an error.
Can you also show your validation methods and what data are you trying to send through form?
i found the solution as mentioned by Ankur Mishra and Aryal,
We have to remember as mentioned by Aryal When validation fails, Laravel makes redirection using GET method to the route it was displayed from. And i displayed my form through below
Route::post('/Complaint/{Complaint}', 'ComplaintsController#save_customer');
Controller method:
public function save_customer($id)
{
$complaint = Complaint::create([
'customer_id'=>$id
]);
// $complaint = Complaint::whereCustomer_id($id)->firstorfail();
return view('complaints.initial_complaint')->with('complaint', $complaint);
}
'complaints.initial_complaint' is the view which has the form which gave me the error of
The GET method is not supported for this route. Supported methods: POST. on submission
So i change POST route to GET :-
Route::middleware(['auth'])->group(function(){
//Route::resource('Complaints', 'ComplaintsController');
Route::get('/Complaint', 'ComplaintsController#find');
Route::get('/Complaint/{Complaint}', 'ComplaintsController#save_customer');
Route::get('/ComplaintGenerate', 'ComplaintsController#generate');
Route::resource('Complaints', 'ComplaintsController');
Route::resource('Occupancies', 'OccupanciesController');
Route::resource('Customers', 'CustomersController');
Route::resource('Services', 'ServiceController');
Route::resource('ServiceTeams', 'ServiceTeamController');
Route::get('/home', 'HomeController#index')->name('home');
});
and in view i passed GET as hidden method
<form action="/ComplaintGenerate" method="POST" >
#csrf
#method('GET')
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<div class="form-row">
<div class="form-group col-md-6">
<label for="complaint_id">Complaint Number</label>
<input type="text" class="form-control" id="complaint_id" name="complaint_id" value="{{$complaint->id}}" readonly >
</div>
<div class="form-group col-md-6">
<label for="complaint_date">Complaint Date</label>
<input type="text" class="form-control" id="complaint_date" name="complaint_date">
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<label for="complaint_description">Complaint Description</label>
<textarea class="form-control" id="complaint_description" name="complaint_description" rows="5"></textarea>
</div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
and now it is working me.. Just posted so if anybody could use it for future reference
you should add
Route::get('/ComplaintGenerate', 'ComplaintsController#generate');
Route::post('/ComplaintGenerate', 'ComplaintsController#generate');

How to edit one column in a row of the database in laravel

How to edit one column in a row of the database in laravel
I can't update one column of row has multiple columns by laravel
My edit :
public function edit($id)
{
$addremark=bookappoitment::findOrFail($id);
return view('admin.ManageTime.addremarks', compact('addremark'));
}
My update:
public function update(Request $request, $id)
{
$request->validate([
'Remarks'=>'required'
]);
$data=bookappoitment::find($id);
$data->Remarks = $request->get('Remarks');
$data->save();
return view('/home');
}
link to update:
Update
form:
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>Remarks :</label>
<textarea class="form-control" rows="10" name="Remarks" placeholder="Remarks">{{ $addremark->Remarks }}</textarea>
</div>
<a class="btn btn-success btn-mini deleteRecord " href="{{route('BookAppoint.update',$addremark->id)}}">Update</a>
</div>
</div>
</div>
You can update 1 (or more, just add to the array) column through the DB class.
DB::table('yourTable')->where('id', $id)->update(['remarks' => $request->input('Remarks')]);
You can also do it for a model like so:
bookappoitment::where('id', $id)->update(['remarks' => $request->input('Remarks')]);
It's in the Laravel documentation here.
<form class="" action="index.html" method="{{ route('BookAppoint.update', $addremark->id) }}">
#method('PATCH')
#csrf
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="Remarks">Remarks :</label>
<textarea name="remarks" rows="10" placeholder="Remarks">{{$addremarks->Remarks}}</textarea>
</div>
</div>
<input type="submit" name="submit" value="Update">
</div>
</div>
</form>

Laravel create custom method to get form data

How do I make custom method to get form data? I want this method same with Laravel update method with parameters request and id. I try this but get error.
In controller
public function updatePassword(Request $request, int $id) {
dd($request->all());
}
In route
Route::post('staffs/{id}/upassword', 'Admin\StaffController#updatePassword')->name('admin.staffs.upassword');
In blade file
<form method="post" accept-charset="utf-8" action="{{ action('Admin\StaffController#updatePassword', ['id' => $staff_id]) }}">
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="password">New Password</label>
<input class="form-control" name="password" type="password">
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="password_confirmation">Confirm New Password</label>
<input class="form-control" name="password_confirmation" type="password">
</div>
</div>
</div>
<input class="btn btn-primary" type="submit">
</form>
I am using Laravel 5.4.
here are some stuff to fix:
First in the tag you can set the action to :
action="route('admin.staffs.upassword', $staff_id)" since it's
easier to write and since you already gave the route a name, so why
not using it ;)
Second add {{csrf_field() }} right before your form closing tag
</form>
what error are you getting? the error is probably because you are not using {{csrf_field()}} after the form declaration, it is needed so that laravel can validate the request. if you want to get the data from the form you can use:
$request->get('inputname');

Laravel: Passing data through a controller results in MethodNotAllowedHttpException

I have seen this post, however I don't believe it is relevant to my issue because I believe I am correctly passing post data through a post route.
Here is the relevant route code:
Route::get('/pass', 'PageController#pass');
Route::post('/pass/{request}',['uses' => 'PageController#passController']);
I would like to have one controller method for the 'pass' page, but to isolate the issue I have separated them.
Here are the relevant methods in PageController.php:
public function pass(){
return view('pass')->with(array(
'title'=>'Create A Pass'
));
}
public function passRequest($request){
$data['request'] = $request;
$validator = Validator::make($request->all(), [
'studentID' => 'required|max:255',
'teacherID' => 'required|max:255',
'destination' => 'required|max:255',
]);
if ($validator->fails()) {
return redirect('/')
->withInput()
->withErrors($validator);
}
$pass = new Pass;
$pass->student = DB::table('users')->where('studentID', $request->studentID)->first()->id;
$pass->teacher = DB::table('users')->where('teacherID', $request->teacherID)->first()->id;
$pass->destination = $request->destination;
$pass->save();
return view('home')->with(array(
'title'=>'Home',
'success'=>'null'
));
}
I used the method stated here in order to pass data to the controller. If this is bad practice/obsolete I'm open to any suggestions.
This is the form in the 'pass' page responsible for sending the post data
<form action="{{ url('pass') }}" method="POST" class="form-horizontal">
{!! csrf_field() !!}
<fieldset>
<!-- Text input-->
<div class="container">
<div class="form-group">
<label class="col-md-4 control-label" for="studentID">Student ID</label>
<div class="col-md-3">
<input id="studentID" name="studentID" type="text" class="form-control input-md">
</div>
</div>
</div>
<!-- Text input-->
<div class="container">
<div class="form-group">
<label class="col-md-4 control-label" for="teacherID">Teacher ID</label>
<div class="col-md-3">
<input id="teacherID" name="teacherID" type="text" class="form-control input-md">
</div>
</div>
</div>
<!-- Text input-->
<div class="container">
<div class="form-group">
<label class="col-md-4 control-label" for="destination">Destination</label>
<div class="col-md-3">
<input id="destination" name="destination" type="text" class="form-control input-md">
</div>
</div>
</div>
<div class="container">
<div class="form-group">
<div class="col-sm-offset-4 col-sm-6">
<button type="submit" class="btn btn-default">
<i class="fa fa-check"></i> Create Pass
</button>
</div>
</div>
</div>
</fieldset>
</form>
On submission of this form I get the MethodNotAllowedHttpException Exception.
If a stack trace of the error would be helpful, please let me know. If there are any suggestions on style, I'm open to that as well.
This form tag will generate a POST request to the URL /pass:
<form action="{{ url('pass') }}" method="POST" class="form-horizontal">
Your routes file does not allow that. It only allows GET requests to that url, but POST requests to /pass/{request}.
Not sure if its just a copy/paste mistake, but your POST route is set up to call PageController#passController method, but the method you shared from your controller is named passRequest. Those will need to match also.
In addition to what Jeff Lambert pointed out, you should not put the {request} variable in the route.
You should remove that and have laravel inject the Request object for you.
Import the Request class if you haven't already at the top of the class.
use Illuminate\Http\Request;
And your function should look like the following...
public function passRequest(Request $request)
{
...
}
If you have additional parameters to pass through the URL, then you may add them to the route, and add the arguments to the method after Request $request. Laravel will figure out what to do with it.
try this one...
Route::post('/pass/post','PageController#passController')->name('post_insert');
in your html form change to ...
<form action="{{ route('post_insert') }}" method="POST" class="form-horizontal">
change it also ...
public function passRequest(Illuminate\Http\Request $request){
....

laravel 5.1 form and routing issue

Hi i am trying to get the output from my form to save and even when i try to echo(print_r) the output it but it simply goes to the post request and does not echo any output.
my route looks like:
Route::controller('stove', 'StoveController', [
'anyData' => 'stove.data',
'getIndex' => 'stove',
]);
Route::get('newstove', 'StoveController#addData');
Route::post('newstove', 'StoveController#store');
my controller:
public function addData()
{
return view('stoves.new');
}
public function store()
{
$input = Request::all();
Stove::create($input);
return redirect('stove');
}
and finally my form is
<form class="form-horizontal" action="/stove">
<fieldset>
<div class="control-group">
<label class="control-label" for="stoveno">Stove Number</label>
<div class="controls">
<input type="text" class="span4" id="stoveno" value="CP001000">
</div> <!-- /controls -->
</div> <!-- /control-group -->
<div class="control-group">
<label class="control-label" for="refno">Ref Number</label>
<div class="controls">
<input type="text" class="span4" id="refno" value="cff001">
</div> <!-- /controls -->
</div> <!-- /control-group -->
<div class="control-group">
<label class="control-label" for="manufacturedate">Manufacture Date</label>
<div class="controls">
<input type="date" class="span4" id="manufacturedate">
</div> <!-- /controls -->
</div> <!-- /control-group-->
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save</button>
<button class="btn">Cancel</button>
</div> <!-- /form-actions -->
</fieldset>
</form>
Thanks
Change the first line of your form to the following..
<form class="form-horizontal" action="/newstove" method="post">
This should submit your form via the POST method to the last route in your routes file.
From what I can see the first part of your routes file is not required...
Route::controller('stove', 'StoveController', [
'anyData' => 'stove.data',
'getIndex' => 'stove',
]);
Route::post('newstove', 'StoveController#store');
You need to add method="post" to the form. and change the action="/stove" to action="/newstove"
Also, isn't it Route::resource for adding controllers to the route list?

Categories