mardi 11 juin 2019

How to show link in datatables column button drop-down menu only when database column condition met

I am attempting to hide the 'resend_welcome' link in a datatables button drop-down menu if a new user has not logged in at least once to the app. I have a "login_count" column on my users table, it defaults to zero. Once a user logs into their account it increases the count per amount of times they have checked in. I am attempting to add an if_statement to the datatables button blade but can't seem to construct one that properly uses the $row->id to hide the link in the dropdown for only those users who have not yet logged in.

I am attempting to build the if-statement such that if the "login_count" equals zero then the link is shown, otherwise the link is hidden. My problem is I can't seem to find the proper way to connect the if-statement conditional with the database row ids. (I also have a "last_login_at" date column that defaults as null, if for some reason it's better to build the conditional on a date where null option rather then the count equals zero option)

My button action.blade:

@can($gateKey.'view')
    <div class="pull-right" style="white-space: nowrap; min-width: 80px;"><div class="btn-group" style="white-space: nowrap;">
            <a href="" class="btn btn-sm btn-default">@lang('global.app_view')</a>
            <a class="btn btn-default btn-sm dropdown-toggle" data-toggle="dropdown" href="#">
                <span class="caret"></span>
            </a><ul class="dropdown-menu pull-right">


                <li><a href="" >@lang('global.app_resend_welcome_letter')</a></li>

            @if(auth()->user()->role->contains(1))
                    <li><a href="" >@lang('global.app_troubleshoot_user')</a></li>
                @endif
                @can($gateKey.'edit')
                    <li><a href="">@lang('global.app_edit')</a></li>
                @endcan
                @can($gateKey.'delete')
                    <li>
                        {!! Form::open(array(
                                'style' => 'display: inline-block;',
                                'method' => 'DELETE',
                                'onsubmit' => "return confirm('".trans("global.app_are_you_sure_delete")."');",
                                'route' => [$routeKey.'.destroy', $row->id])) !!}
                        {!! Form::submit(trans('global.app_delete'), array('class' => 'btn btn-link')) !!}
                        {!! Form::close() !!}
                    </li>
                @endcan

            </ul>
        </div>
    </div>
@endcan

This is the Ajax datatables part of my index.blade:

<script>
        @can('user_delete')
                @if ( request('show_deleted') != 1 )window.route_mass_crud_entries_destroy = ''; @endif
        @endcan
        $(document).ready(function () {
            window.dtDefaultOptions.ajax = '{!! route('admin.users.index') !!}?show_deleted=';
            window.dtDefaultOptions.stateSave = true;
            //window.dtDefaultOptions.scrollY = '50vh';
            window.dtDefaultOptions.scrollCollapse = true;
            window.dtDefaultOptions.columns = [@can('user_delete')
                @if ( request('show_deleted') != 1 )
            {data: 'massDelete', name: 'id', searchable: false, sortable: false},
                @endif
                    @endcan{data: 'name', name: 'name'},
                {data: 'email', name: 'email'},
                {data: 'phone', name: 'phone'},
                {data: 'role.title', name: 'role.title'},
                    @if(auth()->user()->role->contains(1))
                {data: 'team.name', name: 'team.name'},
                    @endif
                {data: 'last_login_at', name: 'last_login_at'},
                {data: 'login_count', name: 'login_count'},

//This is where the Button action ties into the datatables table
                {data: 'actions', name: 'actions', searchable: false, sortable: false}
            ];
            processAjaxTables();
        });
    </script>

I can condition this button link by role or by active user but for whatever reason, I can't seem to find a way to condition this based on database $row->id.

Any thoughts?

Aucun commentaire:

Enregistrer un commentaire