3

I have a form that is using the Laravel forms package, the head of this form looks like this:

{{ Form::model($user, array('route' => array('users.update', $user->username), 'method' => 'PUT')) }}

This form is inside the edit blade at views/users/edit.blade.php

To my knowledge 'route' => array('users.update', $user->$username) does the following: finds a route with the name users.update and adds in the user's username as a parameter.

Inside routes/web.php I have this:

Route::post('/users/{user}', 'UsersController@update')->name('users.update');

Given this I'm assuming the following:

The form goes to the named route and converts it into www.example.com/users/username

However, when I go to the edit page, which has the updating form inside of it, I get:

"Route [users.update] not defined. (View: C:\xampp\htdocs\my-newable\resources\views\users\edit.blade.php)"

Even stranger is that when I run php artisan route:list the route in question is not even listed as a route that you can use within the application.

I have also tried the following command: php artisan route:cache

But it still doesn't appear?

Finally, this is the update method with the UsersController

public function update(Request $request, User $user)
{  
    $user = User::findOrFail($user)->first(); 

    //Validate name, email and password fields  
    $this->validate($request, [

    ]);

    $roles = $request['roles']; //Retreive all roles

    if (isset($roles)) 
    {        
        $user->roles()->sync($roles);  //If one or more role is selected associate user to roles          
    }        
    else 
    {
        $user->roles()->detach(); //If no role is selected remove exisiting role associated to a user
    }

    return redirect()->route('users.index')->with('flash_message', 'User successfully edited.');
}

I'm just not seeing how the users.update method is not defined?

4
  • I'm using $username as the Primary Key in the User model. Commented May 4, 2018 at 16:07
  • As a side note, using strings/varchars as Primary Key in a database is generally a bad idea. You should use an integer. Using strings as Primary Key will slow down your database significantly. Commented May 4, 2018 at 17:18
  • It was more because I wanted to be able to search users by username, and so that the URL slugs contained more human readable elements. If there is a way of having friendly URLs and still using the ID I'd be interested in hearing about that. Commented May 4, 2018 at 17:50
  • Yes there is! You should watch this video: laracasts.com/series/laravel-from-scratch-2017/episodes/31 While you're at it, just watch the entire series. It's good practice. Commented May 4, 2018 at 18:00

1 Answer 1

3

Try php artisan route:clear. According to the documentation php artisan route:cache generates the cached routes file and each time you add a new one this needs to be refreshed. https://laravel.com/docs/5.6/controllers#route-caching

I don't know if route:cache also resets the cached routes. I hope route:clear helps :)

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

1 Comment

route:cache does reset your cache.

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.