1

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="{{ route($routeKey.'.show', $row->id) }}" 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">

{{-- This is the Link I want to build the if-statement around    --}}
                <li><a href="{{route('admin.users.resend_welcome',$row->id)}}" >@lang('global.app_resend_welcome_letter')</a></li>

            @if(auth()->user()->role->contains(1))
                    <li><a href="{{route('admin.impersonate.impersonate',$row->id)}}" >@lang('global.app_troubleshoot_user')</a></li>
                @endif
                @can($gateKey.'edit')
                    <li><a href="{{ route($routeKey.'.edit', $row->id) }}">@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 = '{{ route('admin.users.mass_destroy') }}'; @endif
        @endcan
        $(document).ready(function () {
            window.dtDefaultOptions.ajax = '{!! route('admin.users.index') !!}?show_deleted={{ request('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>

Here is my Controller as requested:

public function index()
    {
        if (! Gate::allows('user_access')) {
            return abort(401, 'Sorry you are not authorized for this action at this time');
        }
        if ($filterBy = Input::get('filter')) {
            if ($filterBy == 'all') {
                Session::put('User.filter', 'all');
            } elseif ($filterBy == 'my') {
                Session::put('User.filter', 'my');
            }
        }


        if (request()->ajax()) {
            $query = User::query();
            $query->with("role");
            $query->with("team");
            $query->with("created_by");
            $query->with("created_by_team");
            $template = 'usersActionsTemplate';
            if(request('show_deleted') == 1) {

                if (!Gate::allows('interest_delete')) {
                    return abort(401, 'Sorry you are not authorized for this action at this time');
                }
                $query->onlyTrashed();
                $template = 'restoreTemplate';
            }


            $query->select([
                'users.id',
                'users.name',
                'users.email',
                'users.phone',
                'users.password',
                'users.team_id',
                'users.remember_token',
//                'users.created_by_id',
                'users.last_login_at',
                'users.login_count',
//                'users.created_by_team_id',
            ]);
            $table = Datatables::of($query);

            $table->setRowAttr([
                'data-entry-id' => '{{$id}}',
            ]);
            $table->addColumn('massDelete', '&nbsp;');
            $table->addColumn('actions', '&nbsp;');
            $table->editColumn('actions', function ($row) use ($template) {
                $gateKey  = 'user_';
                $routeKey = 'admin.users';

                return view($template, compact('row', 'gateKey', 'routeKey'));
            });
            $table->editColumn('phone', function ($row) {
                return $row->phone ? $row->phone : '';
            });
            $table->editColumn('password', function ($row) {
                return '---';
            });
            $table->editColumn('role.title', function ($row) {
                if(count($row->role) == 0) {
                    return '';
                }

                return '<span class="label label-default label-many">' . implode('</span><span class="label label-info label-many"> ',
                        $row->role->pluck('title')->toArray()) . '</span>';
            });
            $table->editColumn('team.name', function ($row) {
                return $row->team ? $row->team->name : '';
            });
            $table->editColumn('remember_token', function ($row) {
                return $row->remember_token ? $row->remember_token : '';
            });
            $table->editColumn('last_login_at', function ($row) {
                return $row->last_login_at ? $row->last_login_at : '';
            });
            $table->editColumn('login_count', function ($row) {
                return $row->login_count ? $row->login_count : '';
            });
//            $table->editColumn('created_by.name', function ($row) {
//                return $row->created_by ? $row->created_by->name : '';
//            });
//            $table->editColumn('created_by_team.name', function ($row) {
//                return $row->created_by_team ? $row->created_by_team->name : '';
//            });

            $table->rawColumns(['actions','massDelete','role.title']);

            return $table->make(true);
        }

        return view('admin.users.index');

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?

The system said this comment was to long to add in the comment section so I will add it to the post instead.

What @if(Auth::user()->login_count > 1) does in this situation is it affects the direct current user. It does not apply the condition to the user records displayed in the datatables index. If the current user has a login_count then it will hide the link for all the different displayed user record row buttons whether or not those user records have any user count.

I need it so that in the table index list of all the different users, those users that have logged in in the past will not have the link displayed in their rows button drop-down menu but any user who has never logged in at all will have visible the 'resend_welcome' link in their rows button drop-down menu.

5
  • What do you mean by $row->id here? please post dependant controller code. Commented Jun 12, 2019 at 5:27
  • If your field inside the users table then you can easily add conditions like @if(Auth::user()->login_count > 1) Commented Jun 12, 2019 at 5:28
  • I have edited the post to include the controller as requested. Commented Jun 12, 2019 at 6:57
  • Comment on using @if(Auth::user()->login_count > 1) The system said the comment was too long to add in the comment section so please see above for my response. Commented Jun 12, 2019 at 7:17
  • I think you like this package. github.com/singlequote/Laravel-datatables Commented Jun 14, 2019 at 14:17

1 Answer 1

1

Just change in your view file, add condition. Like

@if($row->login_count < 1)

    <li><a href="{{route('admin.users.resend_welcome',$row->id)}}" >@lang('global.app_resend_welcome_letter')</a></li>

@endif
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! This is the solution. It is embarrassing how long and how many different options I tried to get this to work, and the solution was this simple and straightforward. Clearly a case of overlooking the basic. I got caught up with auth::user and \App\User:: with different attempts in the blade, controller, and model. I'm embarrassed it was so simple but grateful for your assistance. Thanks
Don't feel embrrassed, as you must learned a lot of things during your search! and happy to assists you :)
The system will not let me mark this answer as useful because I don't have the required 15 reputation points to do so. So I am leaving this comment as my attempt to identify this answer as being both useful and accepted as workable.
@pandorabacchus now you can :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.