I have a MethodNotAllowedHttpException with a button for a post method , it's really strange because i made the same process with a lot of others things in the project but with this route don't want to work . Where i made a mistake ?
thanks a lot in advance friends :)
Here my route :
Route::post('licencies_to_update/{id}', 'LicencieController#Renouveller')->name('licencie.renouveller');
Here my button in my blade view :
{!! link_to_route('licencie.renouveller', 'Effectuer le Renouvellement' , [$licencie->id], ['class' => 'btn btn-primary']) !!}
Here the begin of my controller :
public function Renouveller(Request $request, $id)
{
$licencie = Licencies::findOrFail($id);
dd($licencie);
....
Use:
Route::get('licencies_to_update/{id}', 'LicencieController#Renouveller')
->name('licencie.renouveller');
instead of POST method. Because in the link of your button you are not requesting the url with a POST method but GET method. Besides, you are not doing anything related to POST variable. You are passing a simple variable id in your route param. So, no need to use POST param here.
The link you are creating is an anchor to the route you provided, but the link is a GET request, while you specify in your routes file that you want POST requests on that url.
Either create a form or change the method that route accepts (or let it also accept GET requests)
Edit:
Change your route to
Route::get('licencies_to_update/{id}', 'LicencieController#Renouveller')->name('licencie.renouveller');
to have the expected result the fastest!
Related
My table has the option edit. A row can be updated and saved to the database. While I was trying to implement this option I came across uncertainty. What do I have to do with the data from my edited row when it arrives at my controller? It doesn't seem clear to me do I have to use the edit, the update or combine them both? Do I need edit to find the id of the row that needs to be updated?
I am using the following code in methods to send data to my controller
<template slot="actions" slot-scope="row">
<span #click="updateProduct(row.item);" class="fas fa-pencil-alt green addPointer"></span>
</template>
updateProduct: async function(productData) {
axios.post('/product/update', {
productData: productData
.catch(function(error){
console.log(error)
})
})
}
In my controller, I think I have to find the id. I am pretty sure I am confusing different methods together. Thanks for any input.
public function edit()
{
$product = Product::with('id')->find($id);
// do something with it
}
public function update(Request, $request){
$product->update([
'name' => $request->productData->Name,
'description' => $request->productData->Descr
]);
}
the difference is significant. Edit is for displaying a form to apply changes and Update is used to set them up to server.
Edit is via GET http Update is via PUT http
In Laravel resource controller you can see these two functions "edit" & "update"
For example, you have a resource route 'post'
Edit:
you can return your edit form with your previously stored data
you can call using GET method & URL will be "/post/{id}/edit" and the route will be "post.edit"
update:
you can submit your data which you want to update
you can call using PUT/PATCH method & URL will be "/post/{id}" and the route will be "post.update"
For more information refer : laravel.com -> controllers
I have 2 different views/forms - they are forms where delegates can fill in some information.
view1 allows delegate to fill in one lot of personal info.
view2 allows the delegate to fill in more detailed info.
Both forms have code like this:
{!! Form::open(['action' => ['DelegatesController#update',$delegate->id], 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
{{Form::hidden('_method', 'PUT')}}
{{Form::submit('Submit', ['class' => 'btn btn-primary'])}}
The problem is that both views lead back to the DelegatesController update() method and, as they both only carry partial personal info in the request, the validation fails.
Is there a way to have 2 update() methods in a Controller, each called by separate views? Or a way for the update() method to tell which view called it?
I have been messing around with different routes & different Controller code and put/patch but cannot get it working - I either get methods not allowed or 1 parameter passed instead of 2 depending on what I try to do.
First of all, why don't you just create two separate methods update1() and update2() and then just refer them in your views like DelegatesController#update1 and DelegatesController#update2?
If that's not an option, you can try adding some hidden field e.g. formType, which will indicate for type of form was sent, and then in the update() method of your controller recieve it using request('formType')
If I create a 2nd method in the Controller with EXACTLY the same parameters as the update() method I get the following error message:
Too few arguments to function
App\Http\Controllers\DelegatesController::update_bwofeedback(), 1
passed and exactly 2 expected
The update() method has these parameters:
public function update(Request $request, $delegateid)
so it is indeed wanting 2 parameters but I havent changed anything at the form end in the view except the method name so I am not sure how it passes 2 parameters to update() but not update2()
For the hidden field I did previously try changing:
{{Form::hidden('_method', 'PUT')}}
to:
{{Form::hidden('_method', 'PUT2')}}
but I got an error:
Symfony \ Component \ HttpKernel \ Exception \
MethodNotAllowedHttpException No message
I am now thinking that your hidden field might be different to this hidden method? I will investigate whether I can just incorporate a field/variable in each form and have the update() method do different validation based on that field/variable.
Here the scenario is I want to pass a variable which will be send from one page to another and in next page it's gonna store through a form. So I have passed the variable from first page to second page through the URL. But I want to hide the parameter in the URL. How do I do it?
Here is my route :
Route::get('/registration/{course_id}',[
'uses'=>'AppController#getregistration',
'as'=>'registration'
]);
And Controller :
public function getregistration($course_id)
{
return view('index')->with('course_id',$course_id);
}
And first page this is how I send the value to first page:
<li> A </li>
Post Method
Route
Route::post('/registration',['uses'=>'AppController#getregistration','as'=>'registration']);
View
{!!Form::open(array('url' => '/registration')) !!}
{!! Form::hidden('course_id', '1') !!}
{!! Form::submit('registration') !!}
{!! Form::close() !!}
Controller
public function getregistration(Request $request)
{
$course_id = $request->input('course_id');
return view('index')->with('course_id',$course_id);
}
Get method
use encryption method, it will show encrypted id in url
View
<li> A </li>
Controller
public function getregistration($course_id)
{
$course_id = Crypt::decrypt($course_id);
return view('index')->with('course_id',$course_id);
}
here is no way you hide parameter in url, rather then you convert parameter value encrypt or hash is up to you,
other-way is save value in session first, then call the value from session without define parameter in url.
because laravel route only working to pattern of url /string /id, post get. dynamic value you must be writing / getting using pattern method.
Thanks.
You cannot hide a parameter in URL. If you don't want to show the ID then try using SLUG. I hope you understand what is a SLUG. If you don't then here it is. If you course title is My new Course Title then its slug would be my-new-course-title. And make sure it is unique like an ID in the table. It is also good for SEO, readable and looks good.
I am working on a system and it's working perfectly. Now, i need to create a custom update method which will only update selected columns. The main update works fine but we only need to update a few columns, not every field. So i added two new functions on my EmployeeController on top of the basic index, create, update, store, destroy and delete.
public function editphoto($EmployeeID)
{
$employee=Employee::find($EmployeeID);
return view('employees.editphoto',compact('employee'));
}
public function updatephoto($EmployeeID)
{
return view('hello');
}
On my routes.php file, i added two new routes
Route::resource('employees', 'EmployeesController');
Route::get('employees/{employee}/editphoto', 'EmployeesController#editphoto')->name('employees.editphoto');
Route::get('employees/{employee}', 'EmployeesController#updatephoto')->name('employees.updatephoto');
On my new editphoto.blade.php view
{!! Form::model($employee,['method' => 'PUT','route'=>['employees.updatephoto',$employee->EmployeeID]]) !!}
{!! Form::label('GrandFathersName', 'Grand Fathers Name') !!}
{!! Form::text('GrandFathersName',null,['class'=>'form-control']) !!}
<a class="btn btn-success pull-left form-control" href="{{ URL::route('employees.index') }}">Cancel</a>
{!! Form::close() !!}
When i click the update button on this form, it tries to validate the data, which is actually on the update function of the controller. But i should have gotten a view with a text 'hello'
I thought it was the PATCH method that was causing it to go to the update method, so i tried to change it and even remove it, but it either throws an error or the same thing.
Here is the route list.
I've tried the solution on Add new methods to a resource controller in Laravel even though it is for laravel 4. I didn't try the second answer, although it was not marked as a solution. Besides, You can see that i have added the proper routes on the Controller.
So, how can i create a new update method with a PATCH action request or how can i update the data with a new method with a PUT or any other action request?
Your problem is that the employees.update route already defined by the Route::resource matches the incoming URL path and HTTP verb when you try to update the photo.
There is no difference between the path employees/{employees} defined by the resource and employees/{employee} defined by you, because the path variable name doesn't matter when matching, so it will always match the route registered first. The solution is easy in this case, just use a different path definition for updating the photo, for example:
Route::put('employees/{employee}/updatephoto', 'EmployeesController#updatephoto')->name('employees.updatephoto');
With this change alone your edit photo form should now work.
up until this point I have essentially been using resource routing. One of my routes is for projects. If I create a project and then SHOW it, I see a URL in the form of
myUrl/projects/1
On the show page for a project, I want to be able to add a document. I have set up the relationships so a project can have one document and a document belongs to a project. I then set up the following route to handle the saving of the documents data
Route::post('projects/{id}/docOne', 'DocOneController#store');
So I add an a form in projects/show.blade.php, which opens like so
{!!
Form::model(new App\DocOne, [
'class'=>'form-horizontal',
'route' => ['docOne.store']
])
!!}
I then have my form fields and a save data button. Because of this new form within my projects show page, when I now show a project, it complains that the route for this new form is not defined.
How can I get this route to work within the projects show page?
Thanks
First of all you need to define a route name to your route, if you want to call it by his name.
So your route would be like:
Route::post('projects/{id}/docOne', [ //you need an array to set a route name
'as' => 'docOne.store', //here define the route name
'uses' => 'DocOneController#store' //here the callback
]);
Second you need to change your laravel form to use your route name and set the id
{!! Form::model(new App\DocOne, [
'route' => ['docOne.store', $project], //if you have setted the id variable like $id blade it gonna retturn it automatically only by passing the object, else, you can set $project->id
'method' => 'POST']
) !!}
EDIT:
You can't get an instance of a model on your view.
So the part:
{!! Form::model(new App\DocOne,
gonna fails every time you trye, also, the form:model needs an instance of a class that should have your vars filled with the info that the inputs should have (when you edit it).
You have two solutions:
If it's a new Doc and never before exist on your dataBase
I recomend to change your
Form::model
to:
Form::open
if it's a Doc thath already exist on your DB, like an edit, so in your controller you need to pass your existing Docas $docand remplace the: {!! Form::model(new App\DocOne, to:
{!! Form::model($doc,
and it works.
Form model was created to fill the input values with the data existing in your object instance, like when you edit someting.
So you need to have a correct instance.
Another think it's the MVC scope, a view shouldn't have acces to models, except if are passed by the controller.
Ok that's all.