nasrulhazim
4/26/2018 - 3:07 AM

Laravel: HasDatatable Trait

Laravel: HasDatatable Trait

<?php

namespace App\Traits;

trait HasDatatable
{
	/**
	 * Get Datatable Fields to be display.
	 * 
	 * @return array
	 */
	public function getDatatableFields() : array
	{
		if (! isset($this->datatable)) {
			return ['*'];
		}

		return $this->datatable;
	}
	/**
	 * Datatable scope.
	 * 
	 * @param  \Illuminate\Database\Eloquent\Builder $query
	 * @return \Illuminate\Database\Eloquent\Builder
	 */
	public function scopeDatatable($query)
	{
		return $query->select($this->getDatatableFields());
	}
}