-1

I am new to Laravel and I was reading some code and came across code to update password. But I noticed that they weren't using PUT/PATCH method in the same as opposed to what I had seen in various other examples while writing CRUD such as

<form class="well" method="post" action="{{url('todo/'.$rec->id)}}">
                    {{csrf_field()}}
                    {{method_field("PUT")}}

But in this piece of code mentioned below there wasn't a mention of PUT/PATCH, why is it so? This is the controller code which uses Update()

public function updatePassword(Request $request) {
        if ($request->isMethod('post')) {
            $data = $request->all();
            $user_email=Auth::user()->email;
            $check_password = User::where(['email' => Auth::user()->email])->first();
            $current_password = $data['current_pwd'];
            if (Hash::check($current_password, $check_password->password)) {
                $password = bcrypt($data['new_pwd']);
                User::where('email', $user_email)->update(['password' => $password]);
                return redirect('admin/settings')->with('flash_message_success', 'Password Updated Successfully');
            } 
            else {
                return redirect('admin/settings')->with('flash_message_error', 'Current Password Incorrect');
            }
        }
    }

And this is the HTML Code:

<form class="form-horizontal" method="post" action="{{url('admin/update-pwd')}}" name="password_validate" id="password_validate" novalidate="novalidate">
                <div class="control-group">{{csrf_field()}}
                  <label class="control-label">Current Password</label>
                  <div class="controls">
                    <input type="password" name="current_pwd" id="current_pwd" />
                    <span id="chkpwd"></span>
                  </div>
                </div>
Don'tDownvoteMe
  • 501
  • 2
  • 16
  • 1
    I've come across similar things in my years of development, people will use post requests for pretty much everything, making something? That's a post request. Updating something? That's a post request. Deleting something? Yeah, you guessed it. That's a post request too. I mean it all works, but it really isn't great practice. So choice is yours! –  Feb 04 '19 at 02:13
  • I thought there was some logical explanation as to what is better and why. Also if there are other similar functions such as `delete` or others offered by Laravel. But I suppose this is a 2nd way to do things? – Don'tDownvoteMe Feb 04 '19 at 02:22
  • Without trying to offend anyone, developers are very lazy. Myself included. There are many articles out there you can take a read. Including this great response [here on stackOverflow](https://stackoverflow.com/questions/630453/put-vs-post-in-rest). If you're using GET/PUT/POST/DELETE etc. These are just the design patterns of a RESTful API. –  Feb 04 '19 at 02:28
  • Thanks a lot for the link and yeah, when mind is running in all directions at times I tend to skip on things. Totally agree I should have looked for PUT vs POST not to forget I still have to understand web services. Thank again. – Don'tDownvoteMe Feb 04 '19 at 02:34

1 Answers1

-1

The first thing is @swonder is correct! Developers use a POST request for everything, from Creating, updating, deleting. But it's not the reason why there is no mention of PUT/PATCH request as you want.

I've been developing in Laravel since Laravel 4.2 and recently when I started working with Vue.js to make SPAs, I found out the actual reason why Laravel forces us to use POST request with {{ method_field('PATCH/PUT') }} for creating PUT/PATCH request.

It's actually because of a known bug at Symphony (A framework used by Laravel) and even PHP itself!

A bug is (And you can re-create it all by yourself, right now!) is, You cannot send multipart/form-data kind of requests because in your Controller (or where you process form data), you will get null in $request/$_POST variable of Laravel/PHP.

That's why you cannot actually use PUT/PATCH request for updating resources that have files as input like an image of the product.

But Laravel is something that respects coding standards and cleanliness of the code, that's why you can mention a route in your web.php or api.php with patch or put request type. But you have to use {{ method_field('PUT/PATCH') }} in your form to make Laravel understand that it's a PATCH/PUT kind of request.

I was shocked when I came to know about this because I am a kind of a person who does not like the mess and wants everything perfect!

Adarsh Sojitra
  • 2,059
  • 1
  • 27
  • 50