Laravel - How to save checkbox choice in item listing?

Asked

Viewed 397 times

0

In a contact management system, I have a list of items which have a checkbox each (gmail style) so I can select them and use some action related to the selected ones. What is the best way to save these selected items to each accessed page?

One of the Controllers listing the items:

$request = DB::table('contatos')
            ->select('*')
            ->paginate(15);

        return view('list')
            ->with('request', $request);

And the View:

    <table>
    <tr>
        <th><input type="checkbox" id="seleciona_todos" /></th>
        <th>Nome</th>
        <th>E-mail</th>
    </tr>

    @foreach($r as $p)
    <tr>
        <td><input type="checkbox" value="{{ $p->id }}" /></td>
        <td>{{ $p->nome }}</td>
        <td>{{ $p->email }}</td>
    </tr>
    @endforeach

    </table>
    {!! $r->appends(Input::except('page'))->links(); !!}

- Has pagination

- I use the Laravel

- You will have other admin user accounts moving at the same time

I’ve thought about doing an ajax for a php file that saves and deletes items from an array in a session or even using a table in the database to record the currently selected items (but there will be other users stirring at the moment and I don’t think this would work). What’s the best way to solve this?

  • If you want to see the modifications in real-time Pusher and Laravel echo

  • you can do so in your input: <input type="checkbox" name="selecionados[]" value="{{$p->id}}">and capture the array: $dataForm = $request->all(); dd($dataForm['selected']), the dd() must return a var_dump of the Laravel.

1 answer

0

I believe there are no problems in using the database and ajax even if having other users moving at the moment. If this is the problem you can use DB::transaction() in the method that performs the action of deleting items.

For example:

DB::transaction(function(){

  // ação de deletar, inserir e etc

});

if an error occurs in this action the system will not make the change in the database.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.