Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

php - PUT Request does not function with Request Class in a Laravel 8.x application

On a Laravel 8.x application, a PUT request does not work with the the Standard Request Class.

    save() {
        let formData = this.prepareFormInput('save');

        this.saveBeforeHook();

        axios.put(`/${this.model}/edit`, formData, this.axiosHeaders)
            .then(r => {
                if(r.data.errors) {
                    this.formDialog.errors = r.data.errors;
                } else {
                    if(r.data.record) {
                        this.saveAfterHook(r.data.record);
                    }

                    this.$root.formDialog.show = false;
                    this.$root.formMode = "add";
                }
            }).
            catch(e => {
                if(e.response.data.errors) {
                    this.formDialog.errors = e.response.data.errors;
                }
            });
    }

I have validated that the PUT request is sending the correct parameters: enter image description here

However the request object does not correctly set any posted data. Looing at the request object, none of the data is set.

I have isolated the issue down to a PUT request in Laravel 8.x.

Using a POST request with the same information in Laravel 8.x works fine. enter image description here

However the PUT request does not SET parameters into the Request object: enter image description here

Using the same PUT request in a Laravel 5.x application works fine.

Why are PUT requests not properly functioning in Laravel 8.x?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think this will fix your problem For PUT you want to make your form like

<form action="/example" method="POST">
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

or

<form action="/example" method="POST">
    @method('PUT')
    @csrf
</form>

For more info: Form Method Spoofing

EDIT: If you are using ajax then use method as "POST" and pass _method value as "PUT". For more info: Laravel AJAX PUT & DELETE


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...