0

I'm using Laravel Datatables and use Laravel Permission to set up permissions for it.

I want to add a new column using foreach to render data and researched this post Foreach in Yajra DataTable Laravel. But, it works not according to my idea.

Controller

$families = Family::where('created_by', auth()->user()->id)
                ->latest()
                ->get();
            return DataTables::of($families)
                ->addColumn(
                    'action',
                    function ($row) {
                        $options = '';
                        $status = Family::groupBy('status')->pluck('status');
                        foreach ($status as $item) {
                            $options .= '<a class="dropdown-item" href="#">'.$item.'</a>';
                        }

                        $return =
                            '@can("family.view")
                            <a href="{{ action(\'Admin\FamilyController@show\', [$_id]) }}" class="btn btn-xs btn-info"><i class="fas fa-eye"></i> @lang("messages.view")</a>
                            @endcan

                            @can("family.delete")
                            <button data-href="{{ action(\'Admin\FamilyController@destroy\', [$_id]) }}" class="btn btn-xs btn-danger delete_family_button"><i class="fas fa-trash"></i> @lang("messages.delete")</button>
                            @endcan

                            @can("family.update")
                            <div class="btn-group">
                                <button type="button" class="btn btn-info btn-xs">Hành động</button>
                                <button type="button" class="btn btn-info btn-xs dropdown-toggle dropdown-icon" data-toggle="dropdown">
                                    <span class="sr-only">Toggle Dropdown</span>
                                    <div class="dropdown-menu" role="menu">
                                        '.$options.'
                                    </div>
                                </button>
                            </div>
                            @endcan';
                        return $return;
                })
                ->removeColumn('_id')
                ->rawColumns(['action'])
                ->make(true);

View

enter image description here

Please help me!

Thanks so much.

Hoang Dinh
  • 157
  • 1
  • 10

2 Answers2

1

I'm pretty sure blade directives like can or lang could not be converted in your return string

You can move permission logic outside the string by using Auth facade and -- function for translation files

Something like this :

$user = Auth::user();
$result_string = '';
if($user->can('family.view'))
    $result_string .= 'something that user need to see';
$result_string .= __('messages.delete'); // example for lang substitution
Ivanilov
  • 46
  • 1
0

You can't use blade directive inside your controller, but you can do something like this:

$data_table = Datatables::of( $data )->addColumn( 'actions', function( $row ){
                $btn = '';
                if (Auth::user()->can('edit')) {
                    $btn .= '<a title="Edit" data-id="'.$row->id.'"  href="'.route("your routename",["your parameters"]).'" class=" btn btn-sm btn-primary">Edit</a>'
                }
                return $btn;
            } );

here i added a button to the action column that opens a link if the user has the edit permissions

TTT
  • 358
  • 3
  • 6
  • 16