3

in my laravel application whenever i go to this route: /admin or admin/users I get the following error message but the the other routes work like admin/users/create or admin/users/edit:

Missing required parameters for [Route: admin.users.edit] [URI: admin/users/{id}/edit]. (View: C:\xampp\htdocs\CMS\resources\views\admin\index.blade.php)

To note I'm not including all the html code of index.blade.php as it's too long the only blade code is from what i have pasted.

Index.blade view:

 <ul class="nav nav-second-level">
                            <li>
                                <a href="{{route('admin.users.edit')}}">All Userss</a>
                            </li>

                            <li>
                                <a href="{{route('admin.users.create')}}">Create User</a>
                            </li>

                        </ul>

Route list:

URI:

- admin/users
- admin/users/create
- admin/users/store
- admin/users/update
- admin/users/{id}/edit

Name:

1. admin.users.index
 2. admin.users.create
 3. admin.users.store
 4. admin.users.update
 5. admin.users.edit

Action:

 1. App\Http\Controllers\AdminUsersController@index 
 2. App\Http\Controllers\AdminUsersController@create 
 3. App\Http\Controllers\AdminUsersController@store 
 4. App\Http\Controllers\AdminUsersController@update 
 5. App\Http\Controllers\AdminUsersController@edit

admin/users

Routes:

Route::get('admin/users/', [

    'uses' => 'AdminUsersController@index',
    'as' => 'admin.users.index'
]);


Route::get('admin/users/create', [

    'uses' => 'AdminUsersController@create',
    'as' => 'admin.users.create'
]);

Route::get('admin/users/{id}/edit', [

    'uses' => 'AdminUsersController@edit',
    'as' => 'admin.users.edit'
]);

Controller:

    public function edit($id)
    {

        $user = User::findOrFail($id);

        $roles = Role::pluck('name', 'id')->all();

        return view('admin.users.edit', compact('user', 'roles'));
    }


    public function update(Request $request, $id)
    {
        //
    }

Edit View:

@extends('layouts.admin')

@section('content')

<h1>Edit Users</h1>

{!! Form::open(['method' => 'PATCH', 'action' => ['AdminUsersController@update', $user->id], 'files' => true]) !!}

@include('includes.form-error')

<div class="form-group">

    {!! Form::label('name', 'Name:') !!}
    {!! Form::text('name', null, ['class' => 'form-control']) !!}

</div>


<div class="form-group">

    {!! Form::label('email', 'Email:') !!}
    {!! Form::email('email', null, ['class' => 'form-control']) !!}

</div>


<div class="form-group">

<div class="form-group">

    {!! Form::label('role_id', 'Role:') !!}
    {!! Form::select('role_id',$roles , null, ['class' => 'form-control']) !!}

</div>

<div class="form-group">

    {!! Form::label('status', 'Status:') !!}
    {!! Form::select('status', [1 => 'Active', 0 => 'Not Active'], 0, ['class' => 'form-control']) !!}

</div>

    <div class="form-group">

    {!! Form::label('photo_id', 'Photo:') !!}
    {!! Form::file('photo_id', null, ['class' => 'form-control']) !!}

</div>


<div class="form-group">

    {!! Form::label('password', 'Password:') !!}
    {!! Form::password('password', ['class' => 'form-control']) !!}

</div>

<div class="form-group">

    {!! Form::submit('Create User', ['class' => 'btn btn-primary']) !!}

</div>


{!! Form::close() !!}


@endsection
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
steven
  • 191
  • 2
  • 4
  • 12

3 Answers3

6

Here is your error. You missed to pass the variable in route:

  <li>
      <a href="{{route('admin.users.edit')}}">All Users</a>
  </li>

Instead of {{route('admin.users.edit')}} use:

{{route('admin.users.edit', $id)}}

This is actually editing route. If you want to get all users use:

{{route('admin.users.index')}}

Rashad
  • 1,344
  • 2
  • 17
  • 33
  • Now i get Undefined variable: id (View: C:\xampp\htdocs\CMS\resources\views\admin\index.blade.php) – steven Jan 24 '17 at 23:17
  • Because, you don't have a `$id` variable. Just replace it with `{{route('admin.users.index')}}`. `$id` will be used when you are going to edit selected user. – Rashad Jan 24 '17 at 23:18
  • Right that works. But with /admin/users i get this error: Use of undefined constant id - assumed 'id' (View: C:\xampp\htdocs\CMS\resources\views\admin\users\index.blade.php) – steven Jan 24 '17 at 23:20
  • there should not be any `$id` in `/admin/users`. You put somewhere `$id`, just remove it – Rashad Jan 24 '17 at 23:23
3

Change {{route('admin.users.edit')}} to {{route('admin.users.edit', $id)}} and dont forget to change $id to the real id variable :)

And its strange, because you write All users and giving the edit route :)

Eimantas Gabrielius
  • 1,356
  • 13
  • 15
  • Now i get : Undefined variable: id (View: C:\xampp\htdocs\CMS\resources\views\admin\index.blade.php) – steven Jan 24 '17 at 23:18
1

You are using:

<a href="{{route('admin.users.edit')}}">All Userss</a>

in your Blade file, but your route is defined like so:

Route::get('admin/users/{id}/edit', [

    'uses' => 'AdminUsersController@edit',
    'as' => 'admin.users.edit'
]);

so it's edit action and you need to pas id of user you want to edit for example:

<a href="{{route('admin.users.edit', [auth()->user()->id])}}">All Userss</a>
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • 1
    In case you have more than one parameter in your route, `eg. /{class_id}/{episode_id}` this answer is the one, because of that array notation. – sajed zarrinpour Aug 14 '19 at 11:12