Search code examples
phplaravel-5modellaravel-bladelaravel-form

How do you change the action with Form Model Binding in Laravel?


I have a simple model bound form, and just checked the html generated by the blade, and the action is pointing to the wrong url. Did I write the route wrong? None of the documentation helps with this. The action points to /users not /users/{user}, which is where the route points.

@extends('layout')


@section('content')
    <h1>This is a test.</h1>
    <ul>
        @foreach($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
    {!! Form::model($user, ['method'=>'put', 'route'=>['users.update', $user->id], 'class'=>'form']) !!}

    <div class="form-group">
         {{ csrf_field() }}
        {!! Form::label('Your Name') !!}
        {!! Form::text('name', null, 
            ['required', 'class' => 'form-control', 'placeholder'=>'Your name']
        ) !!}
    </div>

    <div class="form-group">
        {!! Form::label('Your E-mail Address') !!}
        {!! Form::text('email', null, 
            ['required', 'class' => 'form-control', 'placeholder'=>'Your E-mail Address']
        ) !!}
    </div>
    <div class="form-group">
        {!! Form::submit('Submit', ['class'=>'btn btn-primary']) !!}
    </div>
    {!! Form::close() !!}

@stop

A route printout can be found here: http://pastebin.com/4wpMsz4k


Solution

  • Problem solved. It was an issue with the id being improperly formatted. It should have been $user->ID, not $user->id.