Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activate/Deactivate multiple clients in Laravel

I am using Laravel 5.1

I have a client table. Client will create their own account and waiting 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 checkbox in every row.

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

Here is 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');
like image 258
smartrahat Avatar asked Oct 27 '15 18:10

smartrahat


1 Answers

Here is the solution in steps, it reflects what you have explained to me over 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 does 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 selection and therefore we need to pass all clients id we want to activate or deactivate to our controller.

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

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

As you can see in following snapshot when we select client 1 and 2 and click on activate we will see our table in database is update 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 sends the same selected clients but doing to different actions from formAction method.

enter image description here

The Solution

This lead to the following solution:

1- First of all you should correct your routes to accept post, so we can pass checkbox with 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 added 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 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 status with default value in client table in 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 Client model and migration.

Note:

This is not a production code, it is just for demonstration. Do not use it directly on production environment. When you test it on your test environment and when it works, than go a head and implement it on your production environment.

like image 114
Maytham Avatar answered Jan 02 '23 19:01

Maytham