0

I'm trying to list all users and so have created a UsersController which goes like:

class UsersController extends Controller
{

public function show(User $user)
{
    //
    $users = User::all();

    return view('superadmin.user.index', compact('users'));
}

}

And a Route that goes like:

Route::get('/superadmin/user', 'UsersController', function () {
        if(Auth::user()->role[0]->id != 3) {
            return redirect('/home');
        } else {
            return view('superadmin.user.index');
        }
});

and finally trying to call it with:

@foreach ($users as $user)
  {{ $user->username }}
@endforeach

But I'm getting: Parse error: syntax error, unexpected 'class' (T_CLASS), expecting ',' or ';'

On the class UsersController line, so i at least know its getting that far. Ive read all the parse error questions and followed a few tutorials on listing users but everything seems to hang up here.

Where am I going wrong?

Rath Baloth
  • 139
  • 9
  • 3
    You missing a `;` following your `namespace` or a `use ...` statement? – Tim Lewis Mar 23 '18 at 18:30
  • Yeah, something above `UsersController` has a syntax error. Also, your route declaration seems wrong. You're defining what happens on the route as closure, but still passing `UsersController` (without method too) for 2nd argument – DevK Mar 23 '18 at 18:31
  • create you route as in documentation and which version or php and laravel you are using ? – Niklesh Raut Mar 23 '18 at 18:39
  • In your case I suggest you to use middleware instead of inline-check – Alex Slipknot Mar 23 '18 at 18:43

2 Answers2

2

A previous line to class UsersController did not end with semi colon. It is a syntax error. Find the line and add semi colon ;

1

You have many issues:

In route you did not write to which method goes and it is not practical to return the view inside the route you can fix it as the following

Route::get('/superadmin/user/{user}', 'UsersController@show');

and in the controller make the operations you want. If you want to check the rule of user you can make middleware and check the rules.

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
ali
  • 832
  • 6
  • 11