30
loading...
This website collects cookies to deliver better user experience
scope
.cuongnd88/lara-query-kit
using Composer.composer require cuongnd88/lara-query-kit
lara-query-kit
into your application.php artisan vendor:publish --provider="Cuongnd88\LaraQueryKit\LaraQueryKitServiceProvider"
App\Traits\QueryKit.php
is already to pump your performance. Please add QueryKit
into the model. . . .
use App\Traits\QueryKit;
class User extends Authenticatable
{
use Notifiable;
use HasOtpAuth;
use HasGuardian;
use QueryKit;
. . . .
}
insertDuplicate(array $data, array $insertKeys, array $updateKeys)
: Insert new rows or update existed rows.public function upsert()
{
$data = [
['name' => "Dean Ngo", 'email' => '[email protected]', 'mobile' => '84905005533', 'password' => Hash::make('123456')],
['name' => "Robert Neil", 'email' => '[email protected]', 'mobile' => '84905001122', 'password' => Hash::make('123456')],
];
User::insertDuplicate($data, ['name', 'email', 'mobile', 'password'], ['name', 'email', 'mobile']);
}
getTableColumns()
: Get the array of columns.public function listTableColumns()
{
$columns = User::getTableColumns();
dump($columns);
}
exclude(array $columns)
: Retrieve a subset of the output data.$excludable
property on the model.. . . .
use App\Traits\QueryKit;
class User extends Authenticatable
{
use Notifiable;
use HasOtpAuth;
use HasGuardian;
use QueryKit;
protected $excludable = ['deleted_at', 'created_at', 'updated_at'];
. . . .
}
public function listUsers()
{
$data = User::exclude()->get()->toArray();
dump($data);
}
public function listUsers()
{
$users = User::exclude(['deleted_at', 'created_at', 'updated_at'])
->get()->toArray();
dump($users);
}
filter(array $params)
: Get the result with filter conditions.fitler
method on a query builder instance to add where
clauses to the query.$filterable
property should contain an array of conditions that you want to execute searching. The key of filterable array corresponds to table columns, whereas the value is condition to call where
clauses. The most basic condition requires two arguments:The first argument is simple where clause such as: where, orWhere, whereBetween, whereNotBetween, whereIn, whereNotIn, whereNull, whereNotNull, orWhereNull , orWhereNotNull, whereDate, whereMonth, whereDay, whereYear, whereTime
The second argument is an operator, which can be any of the database's supported operators.
operator
is LIKE
, it is for a specified patternfiterable
property, the default clause is where
with =
operator. . . .
use App\Traits\QueryKit;
class User extends Authenticatable
{
use Notifiable;
use HasOtpAuth;
use HasGuardian;
use QueryKit;
protected $filterable = [
'id' => ['whereBetween'],
'email',
'name' => ['orWhere', 'LIKE', '%{name}%'],
];
. . . .
}
public function filter()
{
$where = [
'id' => [1,5],
'email' => '[email protected]',
'name' => 'ngo',
];
$data = User::->filter($where)->get()->toArray();
}
filterableCondition()
and assign the filterable conditionspublic function filter()
{
$filterable = [
'email',
'name' => ['orWhere', 'LIKE', '%{name}%'],
'deleted_at' => ['whereNull'],
];
$where = [
'email' => '[email protected]',
'name' => 'ngo',
'deleted_at' => '',
];
$data = User::filterableCondition($filterable)
->filter($where)
->get()
->toArray();
}
searchFulltext($value, $mode = NATURAL_LANGUAGE)
: Run full-text queries against character-based data in MySQL tables.NATURAL_LANGUAGE
(is default): IN NATURAL LANGUAGE MODENATURAL_LANGUAGE_QUERY
: IN NATURAL LANGUAGE MODE WITH QUERY EXPANSIONBOOLEAN_MODE
: IN BOOLEAN MODEQUERY_EXPANSION
: WITH QUERY EXPANSION$searchable
property should contain an array of conditions that you search full-. . . .
use App\Traits\QueryKit;
class User extends Authenticatable
{
use Notifiable;
use HasOtpAuth;
use HasGuardian;
use QueryKit;
protected $excludable = ['deleted_at', 'created_at', 'updated_at'];
protected $searchable = [
'name', 'address'
];
. . . .
}
public function search()
{
$data = User::searchFulltext('ngo')->exclude()->get()->toArray();
dump($data);
}
searchableCols()
public function search()
{
$data = User::searchableCols(['name', 'address'])
->searchFulltext('ngo')
->exclude()
->get()
->toArray();
dump($data);
}
full-text index
on the table before you run full-text queries on a table. The full-text index can include one or more character-based columns in the table.ALTER TABLE `users` ADD FULLTEXT(`name`, `address`);