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->where('id', 1);
$clients->where('id', '=', 1);
$clients->where('id', '!=', 1);
$clients->where('name', 'LIKE', '%John%');

$clients->whereIn('id', [1, 2, 3]);

$clients->orderBy('created_at'); // Create first at first
$clients->orderBy('created_at', false); // Created last at first

$clients->lastestFirst();

$clients = new Clients;
if($request->ids){
    $clients->whereIn('id', $request->ids);
}else{
    $clients->cancel();
}
return ['clients' => $clients];

$clients = (new Clients(['id', 'name']))->get();

$clients = new Clients(['id', 'name']);
$clients->activeOnly();

$clients->getSQL(); // select id, name from clients where active = 1

$clients = new Clients();
$clients->paginate(5, ['format' => 'csv']);

DataSelector\Selector::macro('whereActive', function(){
    return $this->where('active', true);
});

DataSelector\Selector::macro('whereName', function($name){
    return $this->where('name', 'LIKE', '%' . $name . '%');
});

$clients = new Clients;
$clients->whereActive()->whereName('John');

DataSelector\Selector::defineWhere('active', function(){
    return $this->where('active', true);
});

$clients = new Clients;
$clients->whereActive(); // the "where" is added automatically

$users = new Users;
$users->whereActive();

$clients = new Clients;
$clients->eagerLoading()
        ->add('orders')
        ->add('favourites');

$clients = new Clients;
$clients->with('orders');
$clients->with('orders', ['id', 'date'], 'MONTH(date) = 6 AND YEAR(date) = 2017');
$clients->with('orders', ['id', 'date'], ['date', '>=', Carbon::yesterday()], true);

$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"