2 views to 1 controller update() - laravel

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.

Related

Difference between update and editing in controller - laravel

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

Route Exception with a post Method

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!

Updated Selected Columns in Laravel 5.1 or Create a Custom Patch/PUT update function

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.

Laravel 5 routing within blade

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.

Laravel form actions leads to undefined route

Form actions always confused me as it seemed very simple just to specify the controller however, every time I use it I always get Route [Controller#method] not defined. So I always go and manually make the route then use a url for my forms.
I currently have a route set up as Route::controller('handle/events', 'EventsController') and I'm trying to call the method postAdd from a form like:
{{ Form::open(['action' => 'EventsController#postAdd']) }}
instead of using
['url' => 'handle/events/add'] which is perfectly acceptable given that this is a RESTful route.
When I use the action, Laravel throws Route [EventsController#postAdd] not defined.. The method postAdd in the EventsController also accepts a parameter which I would like to pass in the form.
In the controller, the method is
public function postAdd($staff = false) {
var_dump($staff); // Always false
}
and once again I thought it would be as simple as:
{{ Form::open(['url' => 'handle/events/add'], true) }} however it did not change the value of $staff.
Recap
I would like to change my forms to point to controller methods rather than urls.
I would like to pass a parameter with my forms.
First Problem can be solved by naming your routes.
For example: Route::post('handle/events/add',['as' => 'handle.event.add', 'uses' => 'EventsController#addMethod']);
Then in your form you can do something like this
{{ Form::open(array('route' => 'handle.event.add', 'method' =>'POST'))}}
Now your form will use EventsController#addMethod
Docs named routes
If you want to pass a parameter to the controller method you can define it in your route
Route::get('handle/{event}',['as' => 'handle.event.add', 'uses' => 'EventsController#addMethod'])
Now your addMethod expects a paramter.

Resources