Skip to content

Instantly share code, notes, and snippets.

@mariapaulinar
Last active October 17, 2016 03:49
Show Gist options
  • Save mariapaulinar/edbacd8d561cdff4162758747392c35f to your computer and use it in GitHub Desktop.
Save mariapaulinar/edbacd8d561cdff4162758747392c35f to your computer and use it in GitHub Desktop.
Crear datatables desde controlador
use Yajra\Datatables\Html\Builder; // import class on controller
/**
* Datatables Html Builder
* @var Builder
*/
protected $htmlBuilder;
public function __construct(Builder $htmlBuilder)
{
$this->htmlBuilder = $htmlBuilder;
}
public function getBasic(Request $request)
{
if ($request->ajax()) {
return Datatables::of(User::select(['id', 'name', 'email', 'created_at', 'updated_at']))
->addColumn('actions', function ($user) {
return '<a>Edit</a>'
})
->make(true);
}
$html = $this->htmlBuilder
->parameters([
'language' => ['url' => 'https://cdn.datatables.net/plug-ins/1.10.12/i18n/Spanish.json']
])
->addColumn(['data' => 'id', 'name' => 'id', 'title' => 'Id'])
->addColumn(['data' => 'name', 'name' => 'name', 'title' => 'Name'])
->addColumn(['data' => 'email', 'name' => 'email', 'title' => 'Email'])
->addColumn(['data' => 'created_at', 'name' => 'created_at', 'title' => 'Created At'])
->addColumn(['data' => 'updated_at', 'name' => 'updated_at', 'title' => 'Updated At'])
->addColumn(['data' => 'actions', 'name' => 'actions', 'title' => 'Actions', 'orderable' => false, 'searchable' => false]);
return view('datatables.html.basic', compact('html'));
}
//En la vista
@section('demo')
{!! $html->table() !!}
@endsection
@section('scripts')
{!! $html->scripts() !!}
@endsection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment