sucoms's avatar

How do I pass users id to the modal?

blade

<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                
            </div>
            <div class="modal-body">
                <h2>{{ $modal }}</h2>
            </div>
            <div class="modal-footer">
                <button type="button" class="rem-mod btn btn-secondary" data-dismiss="modal">Zatvori</button>
                {{ Form::open(['action'=> ['PagesController@destroy', Auth::user()->id],'method' => 'POST']) }}
                {{ Form::submit('Obrišite račun', ['class' => 'bck-mod btn btn-danger']) }}    
                {{ Form::hidden('_method', 'DELETE') }}
                {{ Form::close() }}
            </div>
        </div>
    </div>
</div>
<div class="container">
    <div class="panel panel-default">
    <div class="panel-heading">Pretraži korisnike</div>
        <div class="panel-body">
            <div class="form-group">
                <input type="text" name="search" id="search" class="form-control" placeholder="Pretraži korisnike" />
            </div>
            <div class="table-responsive">
                <h3 align="center">Broj korisnika: <span id="total_records"></span></h3>
                <table id="users" class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <th>Prezime</th>
                            <th>Ime</th>
                            <th>Telefon</th>
                            <th></th>
                        </tr>
                    </thead>
                <tbody>

                </tbody>
                </table>
            </div>
        </div>    
    </div>
</div>
$(document).ready(function(){

    fetch_user_data();
    
    function fetch_user_data(query = ''){
        $.ajax({
            url:"{{ route('live_search.action') }}",
            method:'GET',
            data:{query:query},
            dataType:'json',
            success:function(data)
            {
                $('tbody').html(data.table_data);
                $('#total_records').text(data.total_data);
            }
        })
    }
    // 
    $(document).on('keyup', '#search', function(){
        var query = $(this).val();
        fetch_user_data(query);
    });

    $('#users').on('click', '.remove-button', function(){
        var id=$(this).data('id');
        $("#deleteModal").modal("show");
        console.log(id);
    });
    $(document).on('click', '.rem-mod', function(){
    var id = $(this).data('id');
    {
        $.ajax({
            url:"{{route('live_search.destroy')}}",
            method:"delete",
            data:{query:query},
            success:function(data)
            {
                alert(data);
                $('#users').DataTable().ajax.reload();
            }
        })
    }
}); 
});

controller

public function destroy($id){ return $id; $user = Auth::user();

    if ($user->IsAdmin()){
        if($users->delete($id)){
            return redirect()->back(); 
        }
    }else{
    
    if ($user->delete()) {
        Auth::logout();
        $data = array(
            'title' => 'Dobrodošli,',
            'title2' => 'da biste nastavili, ulogirajte se!',

        );
        return view('pages.index')->with($data);
    }
    }
}

public function action(Request $request)
{
        
    if($request->ajax()){
        $output = '';
        $query = $request->get('query');
        if($query != ''){
            $data = DB::table('users')
                ->where('surname', 'like', '%'.$query.'%')
                ->orWhere('name', 'like', '%'.$query.'%')
                ->orWhere('phone', 'like', '%'.$query.'%')
                ->orderBy('id')
                ->get();
        }else {
            $data = DB::table('users')
                ->orderBy('id')
                ->get();
        }
        $total_row = $data->count();
        if($total_row > 0){
            foreach($data as $row){
                $output .= '
                    <tr>
                        <td>'.$row->surname.'</td>
                        <td>'.$row->name.'</td>
                        <td>'.$row->phone.'</td>
                        <td><button type="button" class="remove-button btn btn-danger" data-id="'.$row->id.'">
                        <div class="close">&#120;</div>
                        </button></td>
                    </tr>
                ';
            }
        }else{
            $output = '
                <tr>
                    <td align="center" colspan="5">Nema podataka</td>
                </tr>
            ';
        }
        $data = array(
            'table_data'  => $output,
            'total_data'  => $total_row,
        );

        echo json_encode($data);
    }
}
0 likes
9 replies
Borisu's avatar

You can use the auth helper:

auth()->id()

This will give you the id of the current user.

monum's avatar

DB::table('users')->where(function ($query) use ($user_id) { $query->where('id', '=', $user_id); })->get();

or

use Illuminate\Support\Facades\Auth;

$user = Auth::user();

$id = Auth::id();

sucoms's avatar

@Borisu This is admin page where admin selects user he wishes to delete, when he presses the button(button has that users id), the modal opens but with admins id.

Borisu's avatar

Ah yes I see now. You can try to change the event listener for the click event

$('.remove-button').on('click', function(event){
        var id=$(event.target).data('id');
        $("#deleteModal").modal("show");
        console.log(id);
    });

, to watch the actual button being pressed. That way you call to

$(this).data('id')

will return the currently pressed id.

arthvrian's avatar

you have your user_id here

<td><button type="button" class="remove-button btn btn-danger" data-id="'.$row->id.'">

correct?

and you open your modal with a form with your admin_id

 {{ Form::open(['action'=> ['PagesController@destroy', Auth::user()->id],'method' => 'POST']) }}

correct?

you must find (via javascript) the way to replace your admin_id with your user_id in the form action, before show the modal

sucoms's avatar

@arthvrian yeah I know that. problem is that I can't figure out how to do it. I know I have to use the javascript

arthvrian's avatar

@sucoms if you know your admin_id (and it never change) you can do it by replace it in the form action

let admin_id = ? // may be constant?
let user_id = ? // as @Borisu sugest
let newAction = $('.modal-footer').find('form').attr('action'); // did not find form name/id

newAction = newAction.replace(admin_id, user_id);

$('.modal-footer').find('form').attr('action', newAction);

if the admin_id varies (or you have more that one) is a little bit diferent

Please or to participate in this conversation.