sucoms

sucoms

Member Since 9 Months Ago

Experience Points 1,730
Experience Level 1

3,270 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed 12
Lessons
Completed
Best Reply Awards 0
Best Reply
Awards
  • start-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

22 Nov
8 months ago

sucoms started a new conversation Submit Role Button To The Pivot Table

I've got 2 models, User, Role and a pivot table. In the pivot table are visible all appointed roles to the users(user_id | role_id) Currently in controller it's made that every line has it's own submit button for submitting roles to the database. I made only one button but it does not work, it throws exception "No query results for model [App\User]". Also I'm using ajax, but all that you will see in the code below.

I made it separate because generateUserTable also connects to the destroy and action method. Button is in generateUserTable -

<td><button type="button" id="potvrdi" class="potvrdi-button btn btn-primary" data-id="'.$row->id.'">
                    <div class="potvrdi">Potvrdi</div>
</button></td>

Controller -

public function postUserRole(Request $request)
    {
        if ($request->ajax()) {
            $loged_in_user = User::findOrFail(Auth::user()->id);
            $user = User::findOrFail($request->get('id'));
            $r1 = Role::find(1);
            $r2 = Role::find(2);
            if ($loged_in_user->IsAdmin()) {
                if ($request->get('admin_role')==1) {
                    $user->roles()->attach($r1);
                } else {
                    $user->roles()->detach($r1);
                }
                if ($request->get('korisnik_role')==1) {
                    $user->roles()->attach($r2);
                } else {
                    $user->roles()->detach($r2);
                }
            }
            $data = User::all();
            return json_encode($this->generateUserTable($data));
        }
    }

public function generateUserTable($data)
{
    // return User::find(5)->roles;
    $total_row = $data->count();
    $output = "";
    if ($total_row > 0) {
        foreach ($data as $row) {
            $roleNames = '';
            $userRoles = $row->roles()->pluck('id')->toArray();
            foreach (Role::all() as $roles1) {
                $checked = '';
            // var_dump($userRoles);
            // var_dump($roles1->id);
                    // var_dump($roles1);
                if (in_array($roles1->id, $userRoles, true)) {
                    $checked = 'checked="checked"';
                }
                $roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" '.$checked.' name="role" value="'.$roles1->id.'" class="checkbox'.$roles1->id.'" id="'.$roles1->id.'">'.' ' : '';
            }
            $output .= '
                <tr>
                    <td>'.$row->surname.'</td>
                    <td>'.$row->name.'</td>
                    <td>'.$row->phone.'</td>
                    <td>'.$roleNames.'</td>
                    <td><button type="button" id="potvrdi" class="potvrdi-button btn btn-primary" data-id="'.$row->id.'">
                    <div class="potvrdi">Potvrdi</div>
                    </button></td>
                    <td><button type="button" id="rowId" 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>
        ';
    }
    return array(
        'table_data'  => $output,
        'total_data'  => $total_row,
    );
}

}

View - here is where I wish to "transfer" the submit button. I've put it after the table. Script is in view and ajax for submitting user roles to the database is the second one.

<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>Rola</th>
                            <th></th>
                            <th></th>
                        </tr>
                    </thead>
                <tbody>

                </tbody>
                </table>
            </div>            
            <button type="button" id="potvrdi" class="potvrdi-button btn btn-primary">
                <div class="potvrdi">Potvrdi</div>
            </button>
        </div>    
    </div>
</div>


<script>   
    $(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('click', '.potvrdi-button', function(){
            var id = $(this).data('id');
            var admin_role = $(this).parent().parent().find('td:nth-child(4) .checkbox1:checkbox:checked').length;
            console.log(admin_role);
            var korisnik_role = $(this).parent().parent().find('td:nth-child(4) .checkbox2:checkbox:checked').length;
            console.log(korisnik_role);
            $.ajax({
                url: "{{ route('live_search.postUserRole') }}",
                method: "post",
                data: {
                    id:id,
                    admin_role: admin_role,
                    korisnik_role: korisnik_role,
                    _token:'{{ csrf_token() }}'
                    },
                dataType: 'json',
                success: function(data) {
                    $('.checkbox1').prop('checked', false);
                    $('.checkbox2').prop('checked', false);
                    $('tbody').html(data.table_data);
                    $('#total_records').text(data.total_data);
                }
            })
        })

Routes

Route::group(['middleware' => ['web', 'auth']], function () {
    Route::get('/admin', '[email protected]');
    Route::post('/adminForma', '[email protected]')->name('adminForma');
    Route::get('/users', '[email protected]');
    Route::get('/settings', '[email protected]');
    Route::post('/settings', '[email protected]');
    Route::get('/generateUserTable', '[email protected]');
    Route::post('/generateUserTable', '[email protected]');
    Route::get('/live_search/action', '[email protected]')->name('live_search.action');
    Route::post('/live_search/action', '[email protected]');
    Route::get('/live_search/postUserRole', '[email protected]')->name('live_search.postUserRole');
    Route::post('/live_search/postUserRole', '[email protected]');
    Route::get('/live_search/destroy', '[email protected]')->name('live_search.destroy');
    Route::post('/live_search/generateUserTable', '[email protected]');
    Route::get('/live_search/generateUserTable', '[email protected]')->name('live_search.generateUserTable');
});
19 Nov
9 months ago

sucoms started a new conversation Changing Roles On Admin Panel In Checkbox

In presented controller, it's displaying all the users and his attributes(?) such as name, surname, phone and roles. How do I make it that when I select role in checkbox and press "potvrdi"(submit) it submits to database? Do I have to use jquery, ajax? Thank you

Controller

public function action(Request $request) { if ($request->ajax()) { $query = $request->get('query'); if ($query != '') { $data = User::where('surname', 'like', '%'.$query.'%') ->orWhere('name', 'like', '%'.$query.'%') ->orWhere('phone', 'like', '%'.$query.'%') ->orderBy('id') ->get(); } else { $data = User::orderBy('id') ->get(); } return json_encode($this->generateUserTable($data)); } }

public function generateUserTable($data)
{
    $total_row = $data->count();
    $output = "";
    if ($total_row > 0) {
        foreach ($data as $row) {
            $roleNames = '';
            $userRoles = $row->roles()->pluck('id')->toArray();
            // var_dump($userRoles);
            $checked = '';
            foreach (Role::all() as $roles1) {
                if (in_array($roles1->id, $userRoles)) {
                    $checked = 'checked="checked"';
                }
                $roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" '.$checked.' name="role" value="'.$roles1->id.'" class="checkbox" id="checkboxId">'.' ' : '';
            }
            $output .= '
                <tr>
                    <td>'.$row->surname.'</td>
                    <td>'.$row->name.'</td>
                    <td>'.$row->phone.'</td>
                    <td>'.$roleNames.'</td>
                    <td><button type="button" id="potvrdi" class="potvrdi-button btn btn-primary" data-id="'.$row->id.'">
                    <div class="potvrdi">Potvrdi</div>
                    </button></td>
                    <td><button type="button" id="rowId" 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>
        ';
    }
    return array(
        'table_data'  => $output,
        'total_data'  => $total_row,
    );
}
15 Nov
9 months ago

sucoms left a reply on Populate Checkbox

@AURAWINDSURFING - ive got different model Role.php and using pivot

sucoms left a reply on Populate Checkbox

@aurawindsurfing I know that, I purposely did that. how do I make it that it checks only if user has that role?

sucoms started a new conversation Populate Checkbox

how do I populate each checkbox with its id? Stupid thing to get stuck on but anything would help. Currently it's populating every checkbox.

{ $total_row = $data->count(); $output = ""; if ($total_row > 0) { foreach ($data as $row) { $roleNames = ''; $userRoles = $row->roles; $checked = ''; foreach (Role::all() as $roles1) { if (true) { $checked = 'checked="checked"'; } $roleNames .= $roles1->role != null ? $roles1->role.' '.'<input type="checkbox" '.$checked.' name="role" value="'.$roles1->id.'" class="checkbox" id="checkboxId">'.' ' : ''; } $output .= ' '.$row->surname.' '.$row->name.' '.$row->phone.' '.$roleNames.' '.$userRoles.' x '; } } }

14 Nov
9 months ago

sucoms started a new conversation How To Populate Checkboxes With Data From Database?

I can't find the way to populate checkboxes with data from database. I've got roles table(id and role name), users table(id, name, surname, etc.) and a pivot table(user_id, role_id). How do I populate them that it shows if user is admin/user or both?

Thank you.

controller

{ $total_row = $data->count(); $output = ""; if ($total_row > 0) { foreach ($data as $row) { $roleNames = ''; foreach (Role::all() as $roles1) { $roleNames .= $roles1->role != null ? $roles1->role.' '.''.' ' : ''; // if ($roles1 = id(1)) { // return ''; // } }

        $output .= '
            <tr>
                <td>'.$row->surname.'</td>
                <td>'.$row->name.'</td>
                <td>'.$row->phone.'</td>
                <td>'.$roleNames.'</td>
                <td><button type="button" id="rowId" 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>
    ';
}
return array(
    'table_data'  => $output,
    'total_data'  => $total_row,
);

}

05 Nov
9 months ago

sucoms started a new conversation What Am I Doing Wrong Here?

What am I doing wrong here? It says in network tab it's 500 internal server error but I can not figure out why and what is causing it.

Controller

public function destroy(Request $request){
    if($request->ajax()){
        $loged_in_user = User::findOrFail(Auth::user()->id);
        $user = User::findOrFail($request->get('id'));
        if ($loged_in_user->IsAdmin()){
            if($user->delete()){
                $data = DB::table('users')->orderBy('id')->get();
        
                $this->generateUserTable();

                return json_encode($data);
            }
        }
    }
}

public function action(Request $request)
{
    
    if($request->ajax()){
        $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();
        }

        
        $this->generateUserTable();
        
        echo json_encode($data);
    }
}

public function generateUserTable()
{

    $total_row = $data->count();
        $output = "";
        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" id="rowId" 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);
    


}

view

<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>
                <button type="button" class="bck-mod test btn btn-danger" data-dismiss="modal">Obriši korisnika</button>
                {{-- Form::open(['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');
        $(".test").attr('data-id', id);
        $("#deleteModal").modal("show");
    });
    $(document).on('click', '.bck-mod', function(){
        var id = $(this).data('id');
        $.ajax({
            //url:"{{ route('live_search.destroy') }}",
            url:"/live_search/destroy",
            method:"get",
            data:{id:id},
            dataType:'json',
            success:function(data)
            {
                $('tbody').html(data.table_data);
                $('#total_records').text(data.total_data);
            },
            error: function(data)
            {
                console.log(data);
            }
        })
    });
});

web.php(route)

Route::group(['middleware' => ['web', 'auth']], function() { Route::get('/admin', '[email protected]'); Route::post('/adminForma', ['as' => 'adminForma', 'uses' => '[email protected]']); Route::get('/users', '[email protected]'); Route::get('/settings', '[email protected]'); Route::post('/settings', '[email protected]'); Route::get('/generateUserTable', '[email protected]'); Route::post('/generateUserTable', '[email protected]'); Route::get('/live_search/action', '[email protected]')->name('live_search.action'); Route::post('/live_search/action', '[email protected]'); Route::get('/live_search/destroy', '[email protected]')->name('live_search.destroy'); Route::get('/live_search/generateUserTable', '[email protected]')->name('live_search.generateUserTable'); });

thank you

02 Nov
9 months ago

sucoms started a new conversation "Include" One Function Into Another In Controller?

Is there a way I can one function into another? The code in controller got repetitive so is there a way where I can create new function where I will put essential code and then include it into needed functions.

Controller

public function destroy(Request $request){ if($request->ajax()){ $loged_in_user = User::findOrFail(Auth::user()->id); $user = User::findOrFail($request->get('id')); if ($loged_in_user->IsAdmin()){ if($user->delete()){ $data = DB::table('users')->orderBy('id')->get();

                $total_row = $data->count();
                $output = "";
                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" id="rowId" 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,
                );

                return json_encode($data);
            }
        }
    }
    return "Nope";
}

public function action(Request $request)
{
    if($request->ajax()){
        $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();
        $output = "";
        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" id="rowId" 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);
    }
    return "Nope";
}

public function generateUserTable($query = '')
{
    // Here?
}
31 Oct
9 months ago

sucoms left a reply on Undefined Variable: Request

@Sergiu17 @saurabhd

that's basically the first thing you put when you make the controller

sucoms left a reply on Undefined Variable: Request

figured that out, except that?

sucoms started a new conversation Undefined Variable: Request

What is wrong here?

public function destroy(Request $reqeust){ $user = Auth::user(); if ($request->is('admin')){ if($user->delete()){ return view('pages.index'); } }else{ $user->delete(); Auth::logout(); $data = array( 'title' => 'Dobrodošli,', 'title2' => 'da biste nastavili, ulogirajte se!',

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

sucoms left a reply on Passing Users Id To Modal Using Javascript

So what I should have done in this case is, instead of data-id attribute, I should generate the delete url for each user.

For example:

data-url="' . action('[email protected]', ['id' => $row->id]) . '"

Then in view, remove the action for the form. And in JS on click action do this:

var url = $(this).data('url'); $("#deleteModal form").attr('action', url); $("#deleteModal").modal("show");

sucoms left a reply on How Do I Pass Users Id To The Modal?

@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

sucoms started a new conversation Passing Users Id To Modal Using Javascript

How do I pass users ID to the modal? Now it's just writing the id, but how do i pass it to the modal? How do I make it that it converts to users id in modal? I got stuck, I know it has to do something with jquery but unfortunately I don't know how and what. Thank you.

view

<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'=> ['[email protected]', 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', '.bck-mod', function(){
    var id = $(this).data('id');
        $.ajax({
            url:"{{route('live_search.destroy')}}",
            method:"POST",
            data:{id:id},
            success:function(data)
            {
                alert(data);
                $('#users').DataTable().ajax.reload();
            }
        })
});
});

controller

public function destroy(Request $reqeust, $id){ return $id; $user = Auth::user(); if($reqeust){ if ($user->IsAdmin()){ if($user->delete()){ return view('pages.index'); } }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" id="rowId" 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);
    }
}
30 Oct
9 months ago

sucoms left a reply on How Do I Pass Users Id To The Modal?

@Borisu Figured out where the problem is. First it is not pulling users id from the database, and secondly it is not pushing that id to the modal. Here is my github in case you wanted to have the wider picture - https://github.com/sucoms/multiauthzadatak

sucoms left a reply on How Do I Pass Users Id To The Modal?

@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.

sucoms started a new conversation 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'=> ['[email protected]', 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);
    }
}