khwadj / laravel-utility-classes
工具类集合
Requires
- php: >=7.2.18
- laravel/framework: >=5.8.0
This package is not auto-updated.
Last update: 2024-09-03 13:02:48 UTC
README
免责声明:本包的函数将破坏多对多关系的预加载
由于条目的索引,该包与 belongsToMany 关系不兼容。建议在我提交正确索引集合的解决方案之前不要使用此包。
Laravel >= 5.8 的工具类
本说明书中测试的最高 Laravel 兼容性为:7.9.2
版本 0.2 测试的最大兼容性:Laravel 5.8(硬限制 < 6.1.0 根据 composer.json)
版本 0.2.1 测试的最大兼容性:Laravel 5.8(硬限制 < 7.0.0 根据 composer.json)
版本 0.2.2 测试的最大兼容性:Laravel 5.8(硬限制 < 7.0.0 根据 composer.json)
版本 0.3 测试的最大兼容性:Laravel 6.2(硬限制 < 7.0.0 根据 composer.json)
版本 0.4 测试的最大兼容性:Laravel 7.9.2
推荐版本为 0.4.0 适用于任何版本的 Laravel(>= 5.8)。最小可用版本为 0.3.0。之前的版本不可靠,应避免使用。
Eloquent
用于扩展模型的模型类
基础模型类提供
- 运行时缓存
$model = MyModel::cacheGet('myCacheKey');
MyModel::cacheSet('myCacheKey', $model);
- 检索和缓存
$model = MyModel::find_and_remember(1);
- 检索并缓存为
MyModel::find_and_remember_as(1, 'myCacheKey');
- 如果不可用则检索并缓存
$model = MyModel::find_or_recall(1);
- 自定义构建器以检索和缓存
MyModel::where(...)->get_and_remember_as('All_models_with_that_property');
MyModel::where(...)->first_and_remember_as('One_model_with_that_property');
MyModel::where(...)->first_and_remember();
对模型进行的查询的结果集合作为 khwadj\Eloquent\Collection
实例返回,并按主键索引
小心:这将破坏多对多关系的预加载
由于条目的索引,该包与 belongsToMany 关系不兼容。建议在我提交正确索引集合的解决方案之前不要使用此包。
$models = MyModel::all();
// get the model with primary key = 1
$model_one = $models->get(1);
// check and retrieve from relationships
if ( $model_one->relation_many->has(555) ) {
$related_model = $model_one->relation_many->get(555);
}
自 0.3.0 版本起:从预加载的关系中检索
$models = MyModel::with('relationship_many')->get();
$model_one = $models->get(1);
// check and retrieve from eager-loaded relationships
if ( $model_one->relation_many->has(555) ) {
$related_model = $model_one->relation_many->get(555);
}
注意:PK 由 $model->getKey()
确定,如果您的模型未使用自增 PK,则将为空。您可以更新此函数以返回代表您的模型的任何唯一可字符串化的值。
视图服务提供者
简单地重新声明 gatherData() 函数以提升性能。作为回报,Renderable 参数不能传递到视图中。
在 config > app.php 中
'providers' => [
...
// Illuminate\View\ViewServiceProvider::class,
\Khwadj\View\ViewServiceProvider::class,
...