Boost your Laravel models by Lara Query Kit

Lara Query Kit utilizes the Eloquent model. PHP Trait and Laravel local scope are used to implement this utilitiy

Source: internet

PHP Traits

So, what if a class needs to inherit multiple behaviors? OOP traits solve this problem.

Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).

Laravel local scope

https://github.com/cuongnd88/lara-query-kit

Installation & Configuration

composer require cuongnd88/lara-query-kit

2-Push lara-query-kit into your application.

php artisan vendor:publish --provider="Cuongnd88\LaraQueryKit\LaraQueryKitServiceProvider"

3-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;
. . . .
}

Available methods

insertDuplicate

public function upsert()
{
$data = [
['name' => "Dean Ngo", 'email' => 'dinhcuongngo@gmail.com', 'mobile' => '84905005533', 'password' => Hash::make('123456')],
['name' => "Robert Neil", 'email' => '1111@gmail.com', 'mobile' => '84905001122', 'password' => Hash::make('123456')],
];
User::insertDuplicate($data, ['name', 'email', 'mobile', 'password'], ['name', 'email', 'mobile']);
}

getTableColumns

public function listTableColumns()
{
$columns = User::getTableColumns();
dump($columns);
}

exclude

You should define which model attributes you want to exclude. You may do this using the $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'];. . . .
}

Then, User model just call exclude()

public function listUsers()
{
$data = User::exclude()->get()->toArray();
dump($data);
}

Or pass a array of excludable columns as argument

public function listUsers()
{
$users = User::exclude(['deleted_at', 'created_at', 'updated_at'])->get()->toArray();
dump($users);
}

filter

You may use the fitler method on a query builder instance to add where clauses to the query.
The $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.

Exceptionally, the third argument is required with the operator is LIKE, it is for a specified pattern

For convenience, if you verify only column in fiterable 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}%'],
];
. . . .
}

Hear you go

public function filter()
{
$where = [
'id' => [1,5],
'email' => 'dinhcuongngo@gmail.com',
'name' => 'ngo',
];
$data = User::->filter($where)->get()->toArray();
}

Dynamically, you can call filterableCondition() and assign the filterable conditions

public function filter()
{
$filterable = [
'email',
'name' => ['orWhere', 'LIKE', '%{name}%'],
'deleted_at' => ['whereNull'],
];
$where = [
'email' => 'dinhcuongngo@gmail.com',
'name' => 'ngo',
'deleted_at' => '',
];
$data = User::filterableCondition($filterable)
->filter($where)
->get()
->toArray();
}

searchFulltext

There are four modes of full-text searches:

  • NATURAL_LANGUAGE (is default): IN NATURAL LANGUAGE MODE
  • NATURAL_LANGUAGE_QUERY: IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION
  • BOOLEAN_MODE: IN BOOLEAN MODE
  • QUERY_EXPANSION: WITH QUERY EXPANSION

The $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'
];
. . . .
}

Just do it!

public function search()
{
$data = User::searchFulltext('ngo')->exclude()->get()->toArray();
dump($data);
}

You flexibly add matched columns by searchableCols()

public function search()
{
$data = User::searchableCols(['name', 'address'])
->searchFulltext('ngo')
->exclude()
->get()
->toArray();
dump($data);
}

You must create a 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`);

Once you stop learning you start dying (4S: Search — Summarize — Share — Smile)