hughcube / laravel-knight
Installs: 7 086
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Requires
Requires (Dev)
- ext-zend-opcache: *
- hughcube/laravel-wechat: *
- jenssegers/agent: *
- laravel/framework: *
- laravel/sanctum: *
- orchestra/testbench: *
- phpstan/phpstan: *
- squizlabs/php_codesniffer: *
- dev-master
- 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.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: 2025-06-27 16:06:16 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 aims to streamline the development process, optimize code structure, and offer a range of feature-rich tools to enhance the performance, maintainability, and reliability of Laravel applications. With Laravel Knight, developers can build feature-rich web applications faster, reduce redundant work, and handle common challenges like data concurrency more effectively.
Features
- Enhanced Eloquent Models: Provides advanced caching mechanisms, optimistic locking, and convenient data manipulation methods.
- Optimized Query Builder: Extends Laravel's query builder with powerful search and filtering capabilities.
- Controller Utilities: Offers helpful methods for caching controller actions and managing request parameters.
- Robust Caching: Implements intelligent caching strategies to reduce database load and improve response times.
- Optimistic Locking: A built-in solution to prevent data conflicts in concurrent update scenarios.
- Code Quality Tools Integration: Integrates with PHPUnit, PHPStan, and PHP_CodeSniffer for robust testing and code analysis.
Installing
$ composer require hughcube/laravel-knight -vvv
Usage
Laravel Knight provides several powerful Traits and classes to enhance your Laravel application.
1. Model Enhancements (HughCube\Laravel\Knight\Database\Eloquent\Model
& Traits)
You can extend HughCube\Laravel\Knight\Database\Eloquent\Model
or use
specific traits in your Eloquent Models to gain additional functionalities.
Basic Model Usage (with Caching)
Extend HughCube\Laravel\Knight\Database\Eloquent\Model
to get caching and other utilities. You need to implement getCache()
method to provide a cache store.
<?php namespace App\Models; use HughCube\Laravel\Knight\Database\Eloquent\Model; use Psr\SimpleCache\CacheInterface; use Illuminate\Support\Facades\Cache; class User extends Model { // Implement this method to return your desired cache store public function getCache(): ?CacheInterface { return Cache::store('redis'); // Example: using redis cache } // Example: find a user by ID, will use cache if available public static function findUserById(int $id): ?self { return static::findById($id); } // Example: find multiple users by IDs, will use cache if available public static function findUsersByIds(array $ids): \HughCube\Laravel\Knight\Database\Eloquent\Collection { return static::findByIds($ids); } // Example: get a query builder that bypasses cache public static function getUsersWithoutCache() { return static::noCacheQuery(); } } // Usage $user = User::findUserById(1); // Fetches from cache or database $users = User::findUsersByIds([1, 2, 3]); // Fetches from cache or database $userFromDb = User::getUsersWithoutCache()->find(1); // Always fetches from database
Optimistic Locking
To add optimistic locking to any Eloquent Model, simply use
the OptimisticLock
trait and ensure your database table has a data_version
column (unsigned big integer with a default value, e.g., 1
).
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use HughCube\Laravel\Knight\Database\Eloquent\Traits\OptimisticLock; use HughCube\Laravel\Knight\Exceptions\OptimisticLockException; class Product extends Model { use OptimisticLock; protected $fillable = ['name', 'price', 'data_version']; // ... } // In your migration: // $table->unsignedBigInteger('data_version')->default(1); // Usage: $product = Product::find(1); if ($product) { $product->price = 100.50; try { $product->save(); // Automatically handles optimistic locking and increments data_version echo "Product updated successfully!\n"; } catch (OptimisticLockException $e) { echo "Failed to update: " . $e->getMessage() . " (The record was modified by another process).\n"; // Handle concurrency conflict, e.g., reload data and ask user to retry } catch (\Throwable $e) { echo "An unexpected error occurred: " . $e->getMessage() . "\n"; } } // You can temporarily disable optimistic lock for a specific save operation: $product = Product::find(2); if ($product) { $product->name = 'New Name (No Lock)'; $product->disableOptimisticLock()->save(); // This save will bypass optimistic locking echo "Product updated without optimistic lock.\n"; }
2. Query Builder Enhancements (HughCube\Laravel\Knight\Database\Eloquent\Traits\Builder
)
This trait extends Laravel's Eloquent Query Builder with additional helpful methods. These methods are available when you use HughCube\Laravel\Knight\Database\Eloquent\Model
or when you explicitly use the Builder
trait in your custom query builders.
<?php namespace App\Models; use HughCube\Laravel\Knight\Database\Eloquent\Model; // This model already uses the Builder trait implicitly class Order extends Model { // ... } // Usage: // Find orders with 'keyword' in their name (case-insensitive like) $orders = Order::query()->whereLike('name', 'keyword')->get(); // Find orders with name starting with 'prefix' $orders = Order::query()->whereLeftLike('name', 'prefix')->get(); // Find orders created within a date range $startDate = '2023-01-01'; $endDate = '2023-12-31'; $orders = Order::query()->whereRange('created_at', [$startDate, $endDate])->get(); // Find available (not soft-deleted) orders $availableOrders = Order::availableQuery()->get();
3. Controller Utilities (HughCube\Laravel\Knight\Routing\Controller
& HughCube\Laravel\Knight\Knight\Routing\Action
)
Extend HughCube\Laravel\Knight\Routing\Controller
or use
the Action
trait in your controllers to leverage caching and other request-related utilities.
<?php namespace App\Http\Controllers; use HughCube\Laravel\Knight\Routing\Controller; use Illuminate\Http\Request; class ProductController extends Controller { public function show(Request $request, int $id) { // Cache the result of this action for 60 seconds $productData = $this->getOrSet( $this->getActionCacheKey(__METHOD__), // Unique cache key for this action and parameters function () use ($id) { // Simulate fetching product data from database return ['id' => $id, 'name' => 'Cached Product', 'description' => 'This data is cached!']; }, 60 // Cache TTL in seconds ); return response()->json($productData); } public function update(Request $request, int $id) { // After updating a product, you might want to clear its cache $this->forget($this->getActionCacheKey('ProductController@show', ['id' => $id])); return response()->json(['message' => 'Product updated and cache cleared.']); } }
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.