hughcube / laravel-knight
Installs: 7 680
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/hughcube/laravel-knight
Requires
Requires (Dev)
- ext-bcmath: *
- ext-pdo: *
- ext-zend-opcache: *
- hughcube/laravel-wechat: *
- jenssegers/agent: *
- laravel/framework: *
- laravel/sanctum: *
- orchestra/testbench: *
- phpstan/phpstan: *
- squizlabs/php_codesniffer: *
- dev-master
- v6.0.71
- v6.0.70
- v6.0.69
- v6.0.68
- v6.0.67
- v6.0.66
- v6.0.65
- v6.0.64
- v6.0.63
- v6.0.62
- v6.0.61
- v6.0.60
- v6.0.59
- v6.0.58
- v6.0.57
- v6.0.56
- v6.0.55
- v6.0.54
- v6.0.53
- v6.0.52
- v6.0.51
- v6.0.50
- v6.0.49
- v6.0.48
- v6.0.47
- v6.0.46
- v6.0.45
- v6.0.44
- v6.0.43
- v6.0.42
- v6.0.41
- v6.0.40
- v6.0.39
- v6.0.38
- v6.0.37
- v6.0.36
- v6.0.35
- v6.0.34
- v6.0.33
- v6.0.32
- v6.0.31
- v6.0.30
- v6.0.29
- v6.0.28
- v6.0.27
- v6.0.26
- v6.0.25
- v6.0.24
- v6.0.23
- v6.0.22
- v6.0.21
- v6.0.20
- v6.0.19
- v6.0.18
- v6.0.17
- v6.0.16
- v6.0.15
- v6.0.14
- v6.0.13
- v6.0.12
- v6.0.11
- v6.0.10
- v6.0.9
- v6.0.8
- v6.0.7
- v6.0.6
- v6.0.5
- v6.0.4
- v6.0.3
- v6.0.2
- v6.0.1
- v6.0.0
- v5.1.105
- v5.1.104
- v5.1.103
- v5.1.102
- v5.1.101
- v5.1.100
- v5.1.99
- v5.1.98
- v5.1.97
- v5.1.96
- v5.1.95
- v5.1.94
- v5.1.93
- v5.1.92
- v5.1.91
- v5.1.90
- v5.1.89
- v5.1.88
- v5.1.87
- v5.1.86
- v5.1.85
- v5.1.84
- v5.1.83
- v5.1.82
- v5.1.81
- v5.1.80
- v5.1.79
- v5.1.78
- v5.1.77
- v5.1.76
- v5.1.75
- v5.1.74
- v5.1.73
- v5.1.72
- v5.1.71
- v5.1.70
- v5.1.69
- v5.1.68
- v5.1.67
- v5.1.66
- v5.1.65
- v5.1.64
- v5.1.63
- v5.1.62
- v5.1.61
- v5.1.60
- v5.1.59
- v5.1.58
- v5.1.57
- v5.1.56
- v5.1.55
- v5.1.54
- v5.1.53
- v5.1.52
- v5.1.51
- v5.1.50
- v5.1.49
- v5.1.48
- v5.1.47
- v5.1.46
- v5.1.45
- v5.1.44
- v5.1.43
- v5.1.42
- v5.1.41
- v5.1.40
- v5.1.39
- v5.1.38
- v5.1.37
- v5.1.36
- v5.1.35
- v5.1.34
- v5.1.33
- v5.1.32
- v5.1.31
- v5.1.30
- v5.1.29
- v5.1.28
- v5.1.27
- v5.1.26
- v5.1.25
- v5.1.24
- v5.1.23
- v5.1.22
- v5.1.21
- v5.1.20
- v5.1.19
- v5.1.18
- v5.1.17
- v5.1.16
- v5.1.15
- v5.1.14
- v5.1.13
- v5.1.12
- v5.1.11
- v5.1.10
- v5.1.9
- v5.1.8
- v5.1.7
- v5.1.6
- v5.1.5
- v5.1.4
- v5.1.3
- v5.1.2
- v5.1.1
- v5.1.0
- v5.0.9
- v5.0.8
- v5.0.7
- v5.0.6
- v5.0.5
- v5.0.4
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0.0
- v4.7.23
- v4.7.22
- v4.7.21
- v4.7.20
- v4.7.19
- v4.7.18
- v4.7.17
- v4.7.16
- v4.7.15
- v4.7.14
- v4.7.13
- v4.7.12
- v4.7.11
- 4.7.10
- v4.4.23
- v4.4.22
- v4.4.21
- v4.4.20
- v4.4.19
- v4.4.18
- v4.4.17
- v4.4.16
- v4.4.15
- v4.4.14
- v4.4.13
- v4.4.12
- v4.4.11
- v4.4.10
- v4.4.9
- v4.4.8
- v4.4.7
- v4.4.6
- v4.4.5
- v4.4.4
- v4.4.3
- v4.4.2
- v4.4.1
- v4.4.0
- v4.3.21
- v4.3.20
- v4.3.19
- v4.3.18
- v4.3.17
- v4.3.16
- v4.3.15
- v4.3.14
- v4.3.13
- v4.3.12
- v4.3.11
- v4.3.10
- v4.3.9
- v4.3.8
- v4.3.7
- v4.3.6
- v4.3.5
- v4.3.4
- v4.3.3
- v4.3.2
- v4.3.1
- v4.3.0
- v4.2.26
- v4.2.25
- v4.2.24
- v4.2.23
- v4.2.22
- v4.2.20
- v4.2.19
- v4.2.18
- v4.2.17
- v4.2.16
- v4.2.15
- v4.2.14
- v4.2.13
- v4.2.12
- v4.2.11
- v4.2.10
- v4.2.9
- v4.2.8
- v4.2.7
- v4.2.6
- v4.2.5
- v4.2.4
- v4.2.3
- v4.2.2
- v4.2.1
- v4.2.0
- v4.1.10
- v4.1.9
- v4.1.8
- v4.1.7
- v4.1.6
- v4.1.5
- v4.1.4
- v4.1.3
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.20
- v4.0.19
- v4.0.18
- v4.0.17
- v4.0.16
- v4.0.15
- v4.0.14
- v4.0.13
- v4.0.12
- v4.0.11
- v4.0.10
- v4.0.9
- v4.0.8
- v4.0.7
- v4.0.6
- v4.0.5
- v4.0.4
- v4.0.3
- v4.0.2
- v4.0.1
- v4.0.0
- v3.1.30
- v3.1.29
- v3.1.28
- v3.1.27
- v3.1.26
- v3.1.25
- v3.1.24
- v3.1.23
- v3.1.22
- v3.1.21
- v3.1.20
- v3.1.19
- v3.1.18
- v3.1.17
- v3.1.16
- v3.1.15
- v3.1.14
- v3.1.13
- v3.1.12
- v3.1.11
- v3.1.10
- v3.1.9
- v3.1.8
- v3.1.7
- v3.1.6
- v3.1.5
- v3.1.4
- v3.1.3
- v3.1.2
- v3.1.1
- v3.1.0
- v3.0.24
- v3.0.23
- v3.0.22
- v3.0.21
- v3.0.20
- v3.0.19
- v3.0.18
- v3.0.17
- v3.0.16
- v3.0.15
- v3.0.14
- v3.0.13
- v3.0.12
- v3.0.11
- v3.0.10
- v3.0.9
- v3.0.8
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.1.9
- v2.0.39
- v2.0.38
- v2.0.37
- v2.0.36
- v2.0.35
- v2.0.34
- v2.0.33
- v2.0.32
- v2.0.31
- v2.0.30
- v2.0.29
- v2.0.28
- v2.0.27
- v2.0.26
- v2.0.25
- v2.0.24
- v2.0.23
- v2.0.22
- v2.0.21
- v2.0.20
- v2.0.19
- v2.0.18
- v2.0.17
- v2.0.16
- v2.0.15
- v2.0.14
- v2.0.13
- v2.0.12
- v2.0.11
- v2.0.10
- v2.0.9
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v1.3.12
- v1.3.11
- v1.3.10
- v1.3.9
- v1.3.8
- v1.3.7
- v1.3.6
- v1.3.5
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.10
- v1.2.9
- v1.2.8
- v1.2.7
- v1.2.6
- v1.2.5
- v1.2.4
- v1.2.3
- v1.2.2
- v1.1.1
- v1.1.0
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
This package is auto-updated.
Last update: 2026-02-01 08:58:03 UTC
README
Introduction
Laravel Knight is a powerful helper extension for the Laravel PHP framework, designed to provide a more robust and efficient development experience. It offers enhanced Eloquent models, query builders, controller utilities, caching mechanisms, optimistic locking, middleware collections, and PostgreSQL array support.
Requirements
- PHP 7.4+
- Laravel 6.0+
Features
- Enhanced Eloquent Models: Advanced caching mechanisms, optimistic locking, soft deletes, and convenient data manipulation methods
- Optimized Query Builder: Extended with
whereLike,whereRange, primary key caching queries, and more - PostgreSQL Array Support: Native PostgreSQL array type handling with
whereIntArrayContains,whereTextArrayOverlaps, etc. - Middleware Collection: Authentication, environment guards, IP restrictions, CORS, HSTS, request logging
- Controller Utilities: Action trait with parameter validation, caching, and event dispatching
- OPcache Management: Commands for compiling, clearing, and monitoring OPcache
- Queue Job Tools: Built-in jobs for ping checks, file cleanup, cache GC, and WeChat token refresh
- Mixin Extensions: Extended Collection, Carbon, Str, and Query Builder classes
Installing
composer require hughcube/laravel-knight -vvv
Usage
1. Model Enhancements
Extend HughCube\Laravel\Knight\Database\Eloquent\Model to get caching and other utilities.
<?php namespace App\Models; use HughCube\Laravel\Knight\Database\Eloquent\Model; use Psr\SimpleCache\CacheInterface; use Illuminate\Support\Facades\Cache; class User extends Model { public function getCache(): ?CacheInterface { return Cache::store('redis'); } } // Usage $user = User::findById(1); // Fetch with cache $users = User::findByIds([1, 2, 3]); // Batch fetch with cache $user = User::noCacheQuery()->find(1); // Bypass cache
2. Optimistic Locking
Add optimistic locking to prevent data conflicts in concurrent scenarios.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use HughCube\Laravel\Knight\Database\Eloquent\Traits\OptimisticLock; class Product extends Model { use OptimisticLock; } // Migration: $table->unsignedBigInteger('data_version')->default(1); // Usage $product = Product::find(1); $product->price = 100.50; try { $product->save(); // Auto handles optimistic locking } catch (\HughCube\Laravel\Knight\Exceptions\OptimisticLockException $e) { // Handle conflict } // Disable lock temporarily $product->disableOptimisticLock()->save();
3. Query Builder Extensions
// LIKE queries Order::query()->whereLike('name', '%keyword%')->get(); Order::query()->whereEscapeLike('name', 'keyword')->get(); // Escaped contains Order::query()->whereEscapeLeftLike('name', 'prefix')->get(); // Escaped prefix // Range query Order::query()->whereRange('created_at', ['2023-01-01', '2023-12-31'])->get(); // Available (not soft-deleted) query Order::availableQuery()->get();
4. PostgreSQL Array Queries
// Integer array queries User::query()->whereIntArrayContains('role_ids', [1, 2])->get(); User::query()->whereIntArrayOverlaps('tag_ids', [3, 4])->get(); // Text array queries Post::query()->whereTextArrayContains('tags', ['php', 'laravel'])->get(); // Array length/empty checks User::query()->whereArrayLength('permissions', '>', 0)->get(); User::query()->whereArrayIsNotEmpty('roles')->get();
5. Middleware
// routes/web.php Route::middleware(['knight.only-local'])->group(function () { // Local access only }); Route::middleware(['knight.only-private-ip'])->group(function () { // Private IP only }); Route::middleware(['knight.https'])->group(function () { // Force HTTPS });
Available middleware:
knight.authenticate- Authenticationknight.only-local/knight.only-local-env- Local restrictionsknight.only-prod-env/knight.only-test-env- Environment restrictionsknight.only-private-ip/knight.only-public-ip- IP restrictionsknight.https- Force HTTPSknight.hsts- HSTS headerknight.log-request- Request loggingknight.cors- CORS handling
6. Controller Action Trait
<?php namespace App\Http\Controllers; use HughCube\Laravel\Knight\Routing\Controller; class ProductController extends Controller { public function show(int $id) { return $this->getOrSet( $this->getActionCacheKey(__METHOD__, ['id' => $id]), fn() => Product::find($id), 60 ); } }
7. OPcache Commands
# Compile files to OPcache php artisan opcache:compile # Clear CLI OPcache php artisan opcache:clear-cli # Create preload script php artisan opcache:create-preload
8. Built-in Queue Jobs
use HughCube\Laravel\Knight\Queue\Jobs\PingJob; use HughCube\Laravel\Knight\Queue\Jobs\CleanFilesJob; use HughCube\Laravel\Knight\Queue\Jobs\CacheTableGcJob; // Dispatch jobs PingJob::dispatch(); CleanFilesJob::dispatch('/path/to/clean', '*.log', 7); CacheTableGcJob::dispatch();
Configuration
Publish the configuration file:
php artisan vendor:publish --provider="HughCube\Laravel\Knight\ServiceProvider"
Configure route prefixes in config/knight.php:
return [ 'opcache' => [ 'route_prefix' => false, // Set to enable OPcache routes, false to disable ], 'request' => [ 'route_prefix' => false, // Request log routes ], 'ping' => [ 'route_prefix' => null, // Ping routes ], 'phpinfo' => [ 'route_prefix' => false, // PHPInfo routes ], 'devops' => [ 'route_prefix' => false, // Devops system info routes ], ];
Contributing
You can contribute in one of three ways:
- File bug reports using the issue tracker.
- Answer questions or fix bugs on the issue tracker.
- Contribute new features or update the wiki.
The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable.
License
Laravel Knight is open-sourced software licensed under the MIT license.