2

I am using Laravel framework as a backend API and a few blade PHP files for the front end, specifically for the authentication and the admin panel from the /admin route. In /admin, I display a list of all registered users and buttons next to them. (This page is only visible for users that have their value in Admin column set as true). I want to toggle the Admin status of a user, either promoting or demoting them by clicking the button next to the user name. For this, I tried to use a form submit with get method. I have a method defined inside UserController like this:

public function setAdmin($id) {
    $user = User::find($id);
    $user->admin = !$user->admin;
    if($user->save()) {
        echo "Changed";
    }
    else {
        echo "Could not be changed";
    }
}

I want to call this method from the view on the click of a button.

I tried using a Form to send a request by specifying the action, but it gave an error saying the values passed are less than the expected number of parameters.

{!! Form::open(['action' => ['UserController@setAdmin', $user->id], 'method' => 'POST']) !!}
{{  Form::submit('Submit', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}

I have a route set up explicitly to call this action

Route::post('/admin/users/setAdmin', 'UserController@setAdmin')

Although I am not sure if I have to set an explicit route for this action or if it's possible to call a controller function directly from a view without defining the route.

I have iterated through User Model to display all users:

@if(count($users) > 0) 
    @foreach($users as $user)
        <div class="card">
            {{ $user }}
        </div>


        {!! Form::open(['action' => ['UserController@setAdmin', $user->id], 'method' => 'POST']) !!}
        {{  Form::submit('Submit', ['class' => 'btn btn-primary']) }}
        {!! Form::close() !!}
    @endforeach
@else 
    <h2>No users found!</h2>
@endif

EDIT: Added the foreach section of the blade file. Also I modified the 'action' part of the Form::open() parameters, it was a mistype, the parameters error is still there.

Can someone explain how this can be done?

5
  • 1
    {!! Form::open(['action' => ['UserController@setAdmin', $user->id], 'action' => 'POST']) !!} here parameter name should be method for post Commented May 27, 2019 at 7:12
  • The method for post is setAdmin defined in the UserController, or is it a different parameter you are talking about? Commented May 27, 2019 at 7:21
  • you have written 'action' => 'POST', it should be method not action Commented May 27, 2019 at 7:25
  • can you share the code of you view that is with foreach Commented May 27, 2019 at 7:35
  • Added additional code for foreach and changed 'action' to 'method', the error still persists. Looking for an alternative better solution to this. Commented May 27, 2019 at 7:46

1 Answer 1

2

You are trying to pass a parameter to your route but there is any within its declaration. You need to add it in your route path:

Route::post('/admin/users/setAdmin/{id}', 'UserController@setAdmin')

If you don't want to have an URL like this, you should add a hidden input to your form containing your ID:

{!! Form::open(['action' => ['UserController@setAdmin'], 'method' => 'POST']) !!}

{{ Form::hidden('id', $user->id) }}
{{ Form::submit('Submit', ['class' => 'btn btn-primary']) }}

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

And in your controller's method:

use Request;

/* ... */

public function setAdmin(Request $request) {
    $user = User::find($request->id);

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

1 Comment

<input type="hidden" name="id" value="{{ $user->id }}"> translates to {{ Form::hidden('id', $user->id) }} and it changes the value in the database now.

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.