Laravel Form HTML with Put Method for Put Routes

Laravel form html with PUT method for PUT routes

You CAN add css clases, and any type of attributes you need to blade template, try this:

{{ Form::open(array('url' => '/', 'method' => 'PUT', 'class'=>'col-md-12')) }}
.... wathever code here
{{ Form::close() }}

If you dont want to go the blade way you can add a hidden input. This is the form Laravel does, any way:

Note: Since HTML forms only support POST and GET, PUT and DELETE
methods will be spoofed by automatically adding a _method hidden field
to your form. (Laravel docs)

<form class="col-md-12" action="<?php echo URL::to('/');?>/post/<?=$post->postID?>" method="POST">

<!-- Rendered blade HTML form use this hidden. Dont forget to put the form method to POST -->

<input name="_method" type="hidden" value="PUT">

<div class="form-group">
<textarea type="text" class="form-control input-lg" placeholder="Text Here" name="post"><?=$post->post?></textarea>
</div>

<div class="form-group">
<button class="btn btn-primary btn-lg btn-block" type="submit" value="Edit">Edit</button>
</div>
</form>

Laravel 9 html form submit throws 405. Expects PUT, request is GET

As stated above in my comment:

To send a put request you will need to change the method to POST and add @method('PUT') in your form. This will insert a hidden input for the method. Laravel will then automatically route this request to the method specified by the put route in your routes file.

This is needed because as @ADyson writes, browsers are limited to GET and POST request.

And last but not least, browsers or in this case HTML forms are stupid.
Maybe someday this will be changed, who knows.

How to submit a form using PUT http verb in Laravel

With plain html / blade

<form action="{{ route('casas.update', $casa->id) }}" method="post">
{{ csrf_field() }}
{{ method_field('put') }}

{{-- Your form fields go here --}}

<input type="submit" value="Update">
</form>

Wirth Laravel Collective it may look like

{{ Form::model($casa, ['route' => ['casas.update', $casa->id], 'method' => 'put']) }}
{{-- Your form fields go here --}}

{{ Form::submit('Update') }}
{{ Form::close() }}

In both cases it's assumed that you pass a model instance $casa into your blade template

In your controller

class CasasController extends Controller
{
public function edit(Casa $casa) // type hint your Model
{
return view('casas.edit')
->with('casa', $casa);
}

public function update(Request $request, Casa $casa) // type hint your Model
{
dd($casa, $request->all());
}
}

Laravel The GET method is not supported for this route. Supported methods: PUT. Yet i am using PUT method

You cannot use PUT in the form method attribute:
use "post" and the blade directive @method

<form method="post" action="wipe">
@method('PUT')

However to delete a user, maybe the DELETE method is a better option? (implement the same way as the PUT method)

Laravel Form PUT method not working

You cant use put as the form method. Read the documentation about method spoofing in laravel

{!! Form::model($role, ['route' => ['roles.update', $role->id], 'method' => 'post']) !!}
<input type="hidden" name="_method" value="PUT">

@include('roles.fields')

{!! Form::close() !!}

Laravel 6.8 PUT Method not working, Showing Blank Page

As per your code everything looks good.

  • You already tested PUT & PATCH variations as per expert suggestions here.
  • You can get pointer into route file (web.php) but not into controller's Any function >>> That means pointer is not passing to controller.

Reason for pointer not going into controller from route file

  • Your Path OR Name of controller is wrong / mismatched

  • Controller file is called from another place

Question

  • Any BACKUP FOLDER or BACKUP CONTROLLER files stored there into ?? [ \app\Http\Controllers\ ]

If answer is YES then it might be possible that wrong controller from backup is called from laravel cache. REMOVE Those backup files and Folders from controller folder.

Solution

I think controller PATH is cached and wrong controller is called instead.
Try following commands to clear general cache.

php artisan cache:clear
php artisan route:cache
php artisan config:cache
php artisan view:clear

To clear controller file / path cache. We'll have to regenerate autoload.

Try following command. (This step is important)

composer dumpautoload

If this solves your issue then you can use normal html edit.blade form syntax as following.

<form action="{{ route('certificate.update',$certificate_data->id) }}" method="post">
{{ csrf_field() }}
{{ method_field('PUT') }}

// Other form fields

</form>

On your controller. Your normal code should work like following.

public function update(Request $request, Certificate $certificate)
{
return $certificate;
}

Let me know if this process helps you. Good luck.



Related Topics



Leave a reply



Submit