Skip to main content

nasirkhan/laravel-starter

1362 stars
3 code files
View nasirkhan/laravel-starter on GitHub

composer.json

Open in GitHub
{
//
"require": {
"php": "^7.3|^8.0",
//
"yajra/laravel-datatables": "^1.5"
},
//
}

app/Http/Controllers/Backend/UserController.php

Open in GitHub
use Yajra\DataTables\DataTables;
use App\Http\Controllers\Controller;
 
class UserController extends Controller
{
//
public function index_data()
{
$module_title = $this->module_title;
$module_name = $this->module_name;
$module_path = $this->module_path;
$module_icon = $this->module_icon;
$module_model = $this->module_model;
$module_name_singular = Str::singular($module_name);
 
$module_action = 'List';
 
$$module_name = $module_model::select('id', 'name', 'username', 'email', 'email_verified_at', 'updated_at', 'status');
 
$data = $$module_name;
 
return Datatables::of($$module_name)
->addColumn('action', function ($data) {
$module_name = $this->module_name;
 
return view('backend.includes.user_actions', compact('module_name', 'data'));
})
->addColumn('user_roles', function ($data) {
$module_name = $this->module_name;
 
return view('backend.includes.user_roles', compact('module_name', 'data'));
})
->editColumn('name', '<strong>{{$name}}</strong>')
->editColumn('status', function ($data) {
$return_data = $data->status_label;
$return_data .= '<br>'.$data->confirmed_label;
 
return $return_data;
})
->editColumn('updated_at', function ($data) {
$module_name = $this->module_name;
 
$diff = Carbon::now()->diffInHours($data->updated_at);
 
if ($diff < 25) {
return $data->updated_at->diffForHumans();
} else {
return $data->updated_at->isoFormat('LLLL');
}
})
->rawColumns(['name', 'action', 'status', 'user_roles'])
->orderColumns(['id'], '-:column $1')
->make(true);
}
//
}

resources/views/backend/users/index_datatable.blade.php

Open in GitHub
//
@section('content')
//
<div class="row mt-4">
<div class="col">
<div class="table-responsive">
<table id="datatable" class="table table-hover ">
<thead>
<tr>
<th>#</th>
<th>{{ __('labels.backend.users.fields.name') }}</th>
<th>{{ __('labels.backend.users.fields.email') }}</th>
<th>{{ __('labels.backend.users.fields.status') }}</th>
<th>{{ __('labels.backend.users.fields.roles') }}</th>
<th class="text-right">{{ __('labels.backend.action') }}</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
//
 
@push ('after-styles')
<!-- DataTables Core and Extensions -->
<link rel="stylesheet" href="{{ asset('vendor/datatable/datatables.min.css') }}">
 
@endpush
 
@push ('after-scripts')
<!-- DataTables Core and Extensions -->
<script type="text/javascript" src="{{ asset('vendor/datatable/datatables.min.js') }}"></script>
 
<script type="text/javascript">
$('#datatable').DataTable({
processing: true,
serverSide: true,
autoWidth: true,
responsive: true,
ajax: '{{ route("backend.$module_name.index_data") }}',
columns: [
{data: 'id', name: 'id'},
{data: 'name', name: 'name'},
{data: 'email', name: 'email'},
{data: 'status', name: 'status'},
{data: 'user_roles', name: 'user_roles'},
{data: 'action', name: 'action', orderable: false, searchable: false}
]
});
</script>
@endpush

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.