PHP code example of lati111 / laravel_dataproviders
1. Go to this page and download the library: Download lati111/laravel_dataproviders library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lati111 / laravel_dataproviders example snippets
class Datatable extends Controller
{
// Use the dataprovider trait to indicate that this is a dataprovider
use Dataprovider;
// Method to be called from a route
public function data(Request $request) {
// Get the data from the dataprovider trait.
$data = $this->getData($request);
// Return the data as a JsonResponse
response()->json($data, Response::HTTP_OK);
}
// Implement the getContent method from the dataprovider trait to set the unmodifed data
protected function getContent(Request $request): Builder
{
// Create a query for orders
return Order::select();
}
}
class Datatable extends Controller
{
// Use the dataprovider trait to indicate that this is a dataprovider
use Dataprovider;
// Use the paginatable trait to indicate that this dataprovider should be paginatable
use Paginatable;
// Method to be called from a route
public function data(Request $request) {
// Set the default amount of items per page (normally 10)
$this->setDefaultPerPage(20);
// Get the data from the dataprovider trait.
$data = $this->getData($request);
// Return the data as a JsonResponse
response()->json($data, Response::HTTP_OK);
}
...
}
class Datatable extends Controller
{
// Use the dataprovider trait to indicate that this is a dataprovider
use Dataprovider;
// Use the paginatable trait to indicate that this dataprovider should be paginatable
use Searchable;
// Method to be called from a route
public function data(Request $request) {
// Slows down searches but allows the dataprovider to search on aliased column defined in search fields.
$this->setAliasSearch(true);
// Get the data from the dataprovider trait.
$data = $this->getData($request);
// Return the data as a JsonResponse
response()->json($data, Response::HTTP_OK);
}
// Implement the getSearchFields method from the Searchable trait to set what columns can be searched on
function getSearchFields(): array
{
// Return an array of column names belonging to the model this dataprovider is searching on
return ['id', 'product_name'];
}
...
}
class Datatable extends Controller
{
// Use the dataprovider trait to indicate that this is a dataprovider
use Dataprovider;
// Use the paginatable trait to indicate that this dataprovider should be paginatable
use Sortable;
function __construct() {
//when using an aliased column, you can set it in the column aliases array to replace any sorting of `time_since_order` with `created_at`
$this->columnAliases = ['time_since_order' => 'created_at']
}
// Method to be called from a route
public function data(Request $request) {
// Get the data from the dataprovider trait.
$data = $this->getData($request);
// Return the data as a JsonResponse
response()->json($data, Response::HTTP_OK);
}
// Implement the getAllowedSortColumns method from the Sortable trait to set what columns can be sorted on
function getAllowedSortColumns(): array
{
// Return an array of column names belonging to the model this dataprovider is are allowed to be sorted on
return ['total_price', 'price', 'amount', 'created_at'];
}
...
}
class Datatable extends Controller
{
// Use the dataprovider trait to indicate that this is a dataprovider
use Dataprovider;
// Use the paginatable trait to indicate that this dataprovider should be paginatable
use Filterable;
// Method to be called from a route to get the data
public function data(Request $request) {
// Get the data from the dataprovider trait.
$data = $this->getData($request);
// Return the data as a JsonResponse
response()->json($data, Response::HTTP_OK);
}
// Method to be called from a route to get filter options
public function getFilters(Request $request) {
// Gets either a list of available filters, or a list of available options for a filter if one is specified
$data = $this->getFilterData($request);
// Return the data as a JsonResponse
response()->json($data, Response::HTTP_OK);
}
// Implement the getFilterList method from the Filterable trait to set what filters exist
function getFilterList(): array
{
return [
'customer' => new CustomerFilter(),
'product' => new ProductFilter(),
];
}
...
}
class CustomerFilter implements DataproviderFilterInterface
{
// Apply the filter to a query
public function handle(Builder $builder, string $operator, string $value): Builder
{
// Perform query actions necessary to enforce the filter
$builder->where('customer_id', $operator, $value);
return $builder;
}
// Get the details about this filter matching the right format
public function getInfo(): array
{
return [
'option' => 'customer',
'operators' => [
['operator' => '=', 'text' => 'is'],
['operator' => '!=', 'text' => 'is not'],
],
'options' => Product::distinct()->pluck('customer_id'),
];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.