Ad

Laravel 4 - Route [users.postUpdate] Not Defined

- 1 answer

I'm currently doing a demo application on Laravel4. The demo application have some users in the database. I want to edit them one by one. I have a method "postUpdate", however, during editing (http://localhost/testlaravell/users/5/edit) an user from the list, I see the error occurs -

ErrorException (E_ERROR)
Route [users.postUpdate] not defined. (View: D:\wamp\www\testlaravell\local\app\views\users\edit.blade.php).

I have the codes in the routes.php:

Route::get('/', function()
{
return View::make('hello');
});
Route::get('users/{all}/edit', '[email protected]');
Route::controller('users', 'UserController');

In the UserController.php, I wrote the following script for edit and update:

public function getEdit($id)
{
    //

    $user = User::find($id);
    if (is_null($user))
        {

            return Redirect::to('users/all');
        }
    return View::make('users.edit', compact('user'));
}


/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function postUpdate($id)
{
    //


    $input = Input::all();
    $validation = Validator::make($input, User::$rules);
    if ($validation->passes())
    {
        //$user = User::find($id);

            $user = User::find($id);
            $user->username = Input::get('username');
            $user->name = Input::get('name');
            $user->email = Input::get('email');
            $user->phone = Input::get('phone');
            $user->password = Hash::make(Input::get('password'));
            $user->save();

        return Redirect::route('users.getIndex', $id);
    }
    return Redirect::route('users.getEdit', $id)
        ->withInput()
        ->withErrors($validation)
        ->with('message', 'There were validation errors.');
}

The code under edit.blade.php as below:

@extends('users.user') 

@section('main')

<h1>Edit User</h1>
{{ Form::model($user, array('method' => 'PATCH', 'route' => array('users.postUpdate', $user->id))) }}
<ul>
    <li>
        {{ Form::label('username', 'Username:') }}
        {{ Form::text('username') }}
    </li>
    <li>
        {{ Form::label('password', 'Password:') }}
        {{ Form::text('password') }}
    </li>
    <li>
        {{ Form::label('email', 'Email:') }}
        {{ Form::text('email') }}
    </li>
    <li>
        {{ Form::label('phone', 'Phone:') }}
        {{ Form::text('phone') }}
    </li>
    <li>
        {{ Form::label('name', 'Name:') }}
        {{ Form::text('name') }}
    </li>
    <li>
        {{ Form::submit('Update', array('class' => 'btn btn-info')) }}
        {{ link_to_route('users.getAll', 'Cancel', $user->id, array('class' => 'btn')) }}
    </li>
</ul>
{{ Form::close() }}

@if ($errors->any())
<ul>
    {{ implode('', $errors->all('<li class="error">:message</li>')) }}
</ul>
@endif

@stop

What I missed I don't know. Can someone help me?

Ad

Answer

When using Route:controller you won't have any named routes.

When you run:

php artisan routes

you'll see you will have name empty for controller routes, so you need to create routes separately, for example your postUpdate route:

Route::post('users/update', ['as' => 'users.postUpdate', 'uses' => '[email protected]']);

EDIT

Example route list (however from Laravel 5 - so it might be a bit different)

Route list preview

You should also read carefeully @xAoc answer - probably it will be better solution (I almost never use controller method)

Ad
source: stackoverflow.com
Ad