How is update done in Laravel 5.4 - php

I'm still learning Laravel and I'm working with version 5.4. I'm currently trying to perform an update and want to see the contents of the request using dd, but I get redirected to the view page (strange). I compared the documentation and I seem to be doing right. Below is the captured url when i submit the update form
http://127.0.0.1:8000/tasks/2?_token=gX4bBZoZ0bpMgeQ5uIbLNrIegohvAOUJmPTNjbX0&_method=PUT&employee_id=Harry+Ovie&title=update&description=Testing+task&priority=high&begin=2017%2F06%2F02&end=2017%2F06%2F05
This is my route list
Route::get('/tasks', 'TaskController#index');
Route::get('/tasks/create', 'TaskController#create');
Route::post('/tasks', 'TaskController#store');
Route::get('/tasks/{id}', 'TaskController#show');
Route::get('/tasks/{id}/edit', 'TaskController#edit');
Route::put('/tasks/{id}', 'TaskController#update');
This is the update in my TaskController
public function update(Request $request, $id)
{
dd($request);
}
And this is what my form looks like
<form class="form-horizontal" role="form" action='/tasks/{{$task->id}}'>
{{ csrf_field() }}
<input name="_method" type="hidden" value="PUT">
How do i fix my code?

Try posting your form, right now you're performing a GET.
<form class="form-horizontal" role="form" method="post" action='/tasks/{{$task->id}}'>
HTML forms do not support the use of PUT, PATCH and DELETE (and some others).
That's why the hidden field is added and handled by Laravel to perform these actions on a POST request.

Related

How to use different form with different controller functions and the same route in Laravel

I'm building an app with Laravel
In the same blade file I have different forms. In a div I have two forms.
In one form the user writes something in a textarea. After press the button ok, the page is reloaded and the data stored in a table of database.
The other form appears in a modal and it contains other option that the user can set and after press another button the data should stored in another table of database.
So how I can do this? I tried to route the function in my controller in this way:
Route::post('/anam','AnamnController#store');
Route::post('/anam','AnamnController#storePar');
and the tag form:
<form action="{{ action('AnamController#store') }}" method="post" class="form-horizontal">
<form id="formA" action="{{ action('AnamnesiController#storePar') }}" method="" class="form-horizontal">
But these return to me errors. So what I have to do?
Thanks!
it's impossible to use the same route for two different functions unless you change one by get instead of post for example:
Route::post('/anam','AnamnController#store');
Route::get('/anam','AnamnController#storePar');
but you can make this logic:
Route::post('/anam','AnamnController#store');
and the tag form:
<form action="{{ action('AnamController#store') }}" method="post" class="form-horizontal">
<input name="input_name" value="input-val" />
</form>
in controller:
public function store (){
if(request()->input_name == 'val-1')
this->store1();
else
this->store2();
}
protected function store1 () {
//
}
protected function store2 () {
//
}
You can't have two routes with the same url that has the same route method.
In your case you have two POST methods going to the same url. The second route definition is cancelling out the first.
Rename one of them.

Laravel. MethodNotAllowedHttpException in RouteCollection.php

There is a 2 language version on the site, when you turn on Rus the language is added to the URL "/ru" ie it will be http://site/ru, but at the same time, all attempts to send the form end with an error - "MethodNotAllowedHttpException in RouteCollection.php", in the original languages ​​http://site form Normally Are working
My forms:
<form action="/callback" method="post">
Route::post('/callback', 'ApiController#callback');
By registering that route, you're explicitly asking for a POST request, any other method is not allowed.
If you can't control the incoming request's method, then you should try using
Route::get or Route::any (I wouldn't recommend the last one if you're creating an API).
If you are confused about how routes work, I recommend you to use named routes, so you're always sure you're pointing the form to the right direction:
Route::post('/callback', 'ApiController#callback')->name('api.callback');
And then use it for your form in the view just like
<form method="POST" action="{{ route('api.callback') }}">
Or if you don't want to give it a name, just use the action helper
<form method="POST" action="{{ action('ApiController#callback') }}">

Trying to use PATCH method works with AJAX but not with a regular html form

I'm currently trying out http://altorouter.com/ and it's working well for me so far, except for this one issue I'm having
My route is set up like this:
$router->map( 'PATCH', '/admin/pages', 'pageController#update');
If I use the following jquery, the route works perfectly:
$.ajax({
type: "PATCH",
url: "/admin/pages",
data: {page_items:page_items, page_name: 'test_page'},
success: function(returned_data)
{
console.log(returned_data);
}
});
However, no matter what I put in my HTML I can't get a regular form to submit in a way it accepts as PATCH:
<form action="/admin/pages" method="post">
<input type="hidden" name="form_function" value="edit_theme">
<input type="hidden" name="_METHOD" value="PATCH">
<button type="submit">Save Page</button>
</button>
I've tried "_METHOD", "_method", "method" etc. None of them work.
I've also tried
method="PATCH"
but that only causes it to do a GET.
When I echo the $_SERVER['REQUEST_METHOD'] on the target page I get "PATCH" for the ajax, but just "POST" for the form. Hope someone can help.
In short, you cannot.
As you'll see in the W3 Spec
The only valid methods for HTML based forms are "GET" and "POST".
However you can work around this if you wish, on the server side instead. Theres a great article about how Laravel does it here: Theres no Put/Patch Delete Methods
A quick snippet of code from that article:
<form method="POST" action="" accept-charset="UTF-8">
<input name="_method" type="hidden" value="PUT">
</form>
<form method="POST" action="" accept-charset="UTF-8">
<input name="_method" type="hidden" value="PUT">
</form>
If you are not using Laravel and want to build a form manually, you cannot use PUT/PATCH – there’s just no such methods supported by forms in browsers – it’s only GET and POST. So how does Laravel make it work with {{ Form::create([‘method’ => ‘PUT’]) }}?
Actually, under the hood the generated HTML looks like this:
That’s right, Laravel constructs a hidden field with name _method and
then checks it upon form submittion, routing it to the correct
Controller method.
So if for any reason you would need to build the FORM tag yourself,
don’t put (same applied to patch and delete) – it
just won’t work. Instead add hidden fields, if necessary.
So back to your issue, Altorouter. It appears their documentation is rather lacing the best guide I can find for you is here https://recalll.co/app/?q=rest%20-%20PHP%20detecting%20request%20type%20(GET%2C%20POST%2C%20PUT%20or%20DELETE)%20-%20Stack%20Overflow it might be worth your while finding a better router, as Alto doesn't seem to have been updated in around 3 years.
Managed to find a working solution after digging around in the code. Altorouter's match method actually accepts a method parameter, which doesn't seem to be documented anywhere.
Where I used to have
$match = $router->match();
I now have:
if(isset($_POST['_method']))
{
$match = $router->match(null, $_POST['_method']);
}
else
{
$match = $router->match();
}

form update with file upload laravel

I'm having a bit of an issue when it comes to updating a form and and having an file input. Here is what I am working with.
I have a form in laravel 5.1 which has a post method and a hidden 'Patch' method. This works as is should updating the fields that are in the form. However, when it introduce:
<input type="file" id="profile_picture" name="image_url" />
into the form, i get a:
MethodNotAllowedHttpException in RouteCollection.php line 218:
laravel error. I have tried changing the
<input type='hidden' name='_method' value='PATCH'>
to PUT and it still doesnt like it.
My form looks like this:
<form action='{{url("profiles/$user->id")}}' method="post" class="form-horizontal" enctype="multipart/form-data">
route resource looks like this:
Route::resource('profiles', 'ProfilesController');
I can't figure out what I am missing here...Any help is much appreciated.
I believe it has to do with the exact route you are typing out in the "action" parameter matching up with the profile controller's update method.
Try changing
action'{{url("profiles/$user->id")}}'
to
action='{{ route("profiles.update", $user->id) }}'
Additionally, you could use the Laravel Collective HTML package to simply opening and closing of forms.
Also for POST Request types, you need to send the CSRF token along with your form data. If you are using laravel blade template in your view, you may use
{{ csrf_field() }}
which translates to
<input type="hidden" name="_token" value={{ csrf_token() }}
Please refer the documentation for this.

How to use form element in Laravel 5.2 without using form facade?

As I am new to laravel framework, I have a query, I am using <form> tag in blade template so that I can delete the data from table.
I am using this the below code of form tag to delete the data
<form action="{{ route('admin.states.update',$data->state_id) }}" id="form_sample_2" class="form-horizontal" novalidate="novalidate" method="PUT">
Here I have used method as PUT, but browser is automatically considering it as GET request, I found some questions on stackoverflow where many of them said PUT & DELETE is not detected by browser.
So using Laravel Facade Form , this problem is solved
{!! Form::open(array('route'=>['admin.states.update',$data->state_id],'role'=>'form','method'=>'PUT')) !!}
The above code work as intended but my query is I don't want to use Formfacade in Laravel , I want to use first type of HTML code for form opening.
Is there any other method by which I can use PUT method in HTML Form Tag without using any Form FAcade in Laravel.
set form method to post and add a hidden input as following
<input type="hidden" name="_method" value="put">
and also make sure to add
<input type="hidden" name="_token" value="{{ csrf_token() }}">
If your ValidateCSRF middleware is enabled.

Categories