Search code examples
phplaravel-5checkbox

Activate/Deactivate multiple clients in Laravel


I am using Laravel 5.1

I have a client table. The client will create their account and wait for admin approval. Admin can activate, deactivate, or block them anytime. I have three links (I can change to button if needed) bottom of the table ACTIVE, DEACTIVATE & BLOCK. There is also a checkbox in every row.

Now, I want to select one or multiple clients and change the value of the status field of the member table in the database.

Here is a code of my view:

<table border="1">
    <tr>
        <td></td>
        <td>Name</td>
        <td>Activity</td>
        <td>main Activity</td>
        <td>Legal Status</td>
        <td>SIREN Code</td>
        <td>Contact Firstname</td>
        <td>Contact Lastname</td>
        <td>Contact Email</td>
        <td>Contact Telephone</td>
        <td>Address</td>
        <td>Town</td>
        <td>Area</td>
        <td>Region</td>
        <td>Creation Date</td>
        <td>Offer Pack Name</td>
        <td>Login Password</td>
        <td>Status</td>
    </tr>
    @foreach($clients as $client)
    <tr>
        <td>{!! Form::checkbox('client[]',$client->id,null) !!}</td>
        <td>{{ $client->name }}</td>
        <td>{{ $client->activity }}</td>
        <td>{{ $client->main_activity }}</td>
        <td>{{ $client->status }}</td>
        <td>{{ $client->siren_code }}</td>
        <td>{{ $client->contact_firstname }}</td>
        <td>{{ $client->contact_lastname }}</td>
        <td>{{ $client->email }}</td>
        <td>{{ $client->contact_telephone }}</td>
        <td>{{ $client->address }}</td>
        <td>{{ $client->town }}</td>
        <td>{{ $client->area }}</td>
        <td>{{ $client->region }}</td>
        <td>{{ $client->creation_date }}</td>
        <td>{{ $client->offer_pack_name }}</td>
        <td>{{ $client->password }}</td>
        <td>{{ $client->status }}</td>
    </tr>
    @endforeach
</table>
<div class="col-md-10 text-center">
    <a href="{{ action('AdminController@blockClient') }}" class="btn btn-default">BLOCK</a>
    <a href="{{ action('AdminController@activateClient') }}" class="btn btn-default">ACTIVATE</a>
    <a href="{{ action('AdminController@deactivateClient') }}" class="btn btn-default">ACTIVATE</a>
</div>

My route is:

get('blockClient','AdminController@blockClient');
get('activateClient','AdminController@activateClient');
get('activateClient','AdminController@deactivateClient');

Solution

  • Here is the solution in steps, it reflects what you have explained to me in the comments.

    ##Foreword First of all, you will have two functions, not three, activate and deactivate, I assume deactivate is the same as blocking a user.

    Now I would like to explain how this works, normally when you submit a form in Laravel, you can post the data by the form and fetch the data in your controller for further process. In our case, we want to update single or multiple selections and therefore we need to pass all client IDs we want to activate or deactivate to our controller.

    Since we might have more than one client then we need to pass all selected clients to the controller as an array as you did, which is great.

    In our controller, we go through all selected clients and activate or deactivate them depending on our button action.

    As you can see in the following snapshot when we select clients 1 and 2 and click on activate we will see our table in the database is updated to 1 for both selected clients.

    ##How to test it? For debugging you could use dd($results); before $this->activateClient($results); to see which data is submitted by clicking on activate.

    Both activate and deactivate buttons send the same selected clients but do different actions from the formAction method.

    enter image description here

    ##The Solution

    This led to the following solution:

    1- First of all you should correct your routes to accept posts, so we can pass the checkbox with the client ID to the controller function:

    get('admin','AdminController@index');
    post('admin/action', array('uses' => 'AdminController@formAction'));
    

    the admin path will view all clients list, this is only for demo and learning, then you can later change it as it fits your project structure.

    2- In your admin controller add the following functions and remember to add use App\client; after the namespace in your AdminController:

    public function index()
    {
        $client = Client::all();
    
        return view('Admin', ['client' => $client]);
    }
    
    public function formAction()
    {
        $results = Input::get('status');
        if (Input::get('activate'))
        {
            $this->activateClient($results);
        } elseif (Input::get('Deactivate'))
        {
            $this->deactivateClient($results);
        }
    
        return redirect('admin');
    }
    
    public function activateClient($client)
    {
        foreach ($client as $clientId)
            Client::findOrNew($clientId)->update(['status' => "1"]);
    }
    
    public function deactivateClient($client)
    {
        foreach ($client as $clientId)
            Client::findOrNew($clientId)->update(['status' => "0"]);
    }
    

    3- Create admin view and call it admin.blade.php to view all our clients and we can activate or deactivate each of them, if I was you I will make a status with the default value in the client table in the database to ZERO (0):

    <p>Admin site | client activate and deactivate page:</p>
    {!! Form::open(array('url' => 'admin/action')) !!}
    
    <table width="800px">
        <tr>
            <td width="10%">Select</td>
            <td width="20%">Client id</td>
            <td width="20%">Client status</td>
            <td width="50%">Client name</td>
        </tr>
    
        @foreach ($client as $clientOutput)
            <tr>
                <td>{!! Form::checkbox('status[]', $clientOutput->id, false, ['id' => $clientOutput->id]) !!}</td>
                <td>{{$clientOutput->id}}</td>
                <td>{{$clientOutput->status}}</td>
                <td>{{$clientOutput->client}}</td>
            </tr>
        @endforeach
    
    </table>
    
    {!! Form::submit('Activate', ['name' => 'activate']) !!}
    {!! Form::submit('Deactivate', ['name' => 'Deactivate']) !!}
    {!! Form::close() !!}
    

    ##The results And here how it looked like in my test browser, you can check 1 or more clients and activate or deactivate them at once :

    enter image description here

    I assume that you have a Client model and migration.

    ###Note: This is not a production code, it is just for demonstration. Do not use it directly in the production environment. When you test it on your test environment and when it works, then go ahead and implement it on your production environment.