PHP code example of tamkeenlms / laravel-data-selector
1. Go to this page and download the library: Download tamkeenlms/laravel-data-selector 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/ */
tamkeenlms / laravel-data-selector example snippets
class Client extends Model{
public $table = 'clients';
protected $guarded = [];
public function orders(){
return $this->hasMany(Order::class);
}
}
public function scopeActiveOnly($query){
return $query->where('active', true);
}
public function scopeRegisteredThisMonth($query){
return $query->whereMonth('created_at', \Carbon::now());
}
namespace app\selectors;
class Clients extends DataSelector\Selector{
public function __contructor(){
parent::__construct(Client::class);
}
}
$clients = new Clients;
$clients->get(); // All clients (Collection)
namespace app\selectors;
class Clients extends DataSelector\Selector{
public function __contructor(array $columns = null, $withDeleted = false){
$defaultColumns = ['id', 'name', 'created_at']; // The default, in case the instance didn't provide a list
parent::__construct(Client::class, $columns, $defaultColumns, $withDeleted);
}
}
$clients = new Clients(['id', 'name']); // Will select only the id and the name for each Client (excluding the trashed ones)
$allClients = new Clients(['*'], true); // Will select * from all clients, including the trashed
public function activeAndNew(){
$this->where('active', true)
->where('created_at', '=>', Carbon::parse('2018-01-01'));
}
$clients = (new Clients())->activeAndNew()->get();
$clients->select(['id', 'name']);
$clients->select('id, name, LEFT(bio) AS `bio`'); // You can also provide a raw statement
$clients->select(['*'], true); // Overrides the above
$clients = new Clients;
$clients->formatters()->add('name', function($name){ // Where "name" is the name of the column targeted for formatting
return 'Mr. ' . $name
});
$clients->format('name', ...)
DataSelector\Formatter::setGlobalFormatter('YMD', function($date){
return $date->format('Y-m-d');
});
$clients = new Clients;
$clients->format('created_at', 'YMD'); // Will return "created_at_formatted" with the time formatted as Y-m-d
$clients = new Clients;
$clients->with('orders', ['id', 'date']);
$clients->format('orders.date', 'YMD'); // Where "date" is a property of the eager-loaded values of "orders"
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.