1

How write this code in simple html form in laravel blade

{!! Form::model($user, [
          'method' => 'PUT',
          'url'  => '/edit-account'    
]) !!}

In controller have this method:

public function edit(Request $request)
{
    $user = $request->user();
    return view('backend.home.edit', compact('user'));
}
3
  • I don't think i understand? It's just HTML, so, <form method="post" action"/edit-account"> </form> Commented Aug 21, 2017 at 8:32
  • I think you pass hidden field with $user like <input type="hidden" value="{{$user}}" name="user[]"> Commented Aug 21, 2017 at 8:36
  • 1
    Have you actually tried writing a “simple HTML form”? Commented Aug 21, 2017 at 8:50

3 Answers 3

1
<form method="post" action="/edit-account">
    {{csrf_field()}}
    <input type="hidden" name="_method" value="PUT">
</form>

Explanation

"method" in laravel collective form is similar to "method" attribute of html form. "url" in laravel collective form is "action" attribute of html form.

As there are no put, patch and delete method we need to pass a hidden field named "_method" with value of "PUT" for updating data.

And anytime while defining HTML form in the application, we should include a hidden CSRF token field in the form so that the CSRF protection middleware can validate the request and here I am using csrf_field helper to generate the token field.

Sign up to request clarification or add additional context in comments.

3 Comments

You might want to explain why for a better answer.
get this error Class App\Http\Requests does not exist, i have App\Http\Requests
I think that App\Http\Requests is actually a namespace. So instead of using App\Http\Requests. You should use for your requests use Illuminate\Http\Request;.
1

If you want to create a form with a specific method you can do something like this :

<form action="/edit-account" method="POST">
    <input type="hidden" name="_token" value="{{ csrf_token() }}" />
    <input type="hidden" name="_method" value="PUT" />
    [... Inputs like <input type="email" name="email" value="old('email')/> ...]
</form>

or

<form action="/edit-account" method="POST">
    {{ csrf_field() }}
    {{ method_field('PUT') }}
    [... Inputs like <input type="email" name="email" value="old('email')/> ...]
</form>

Documentation

And if you want to see how to validate your form after submit: https://laravel.com/docs/5.4/validation

Comments

0

Form::model basically populates the input fields with data either from old input or given model's attributes. So the basic form can be written like this.

<form action="/edit-account" method="POST">

    <input type="hidden" name="_token" value="{{ csrf_token() }}">

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

    ...

</form>

And for populating the data, you can use old helper function and then check for errors.

So a basic input block would look like this.

<input type="text" name="name" value="{{ old('name', $user->name) }}">

@if ($errors->has('name'))
    <strong>{{ $errors->first('name') }}</strong>
@endif

References:

old()

csrf_token()

Working With Error Messages

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.