mookofe/laravel-support
Composer 安装命令:
composer require mookofe/laravel-support
包简介
Awesome enhancements for your current Laravel Models, Collections and more.
README 文档
README
Awesome enhancements for your current Laravel Models, Collections.
Features
- Simple setup
- Awesome new features for your current Models
- New features for your Collections
Requirements
- illuminate/support: 5.*
Version
1.0.1
Installation
Preparation
Open your composer.json file and add the following to the require array:
"mookofe/laravel-support": "1.*"
Install dependencies
$ php composer install
Or
$ php composer update
Integration
Change inheritance on your models, instead of using the default Eloquent Model change as follow:
<?php namespace App; use Mookofe\LaravelSupport\Model; class User extends Model { }
Using Model features:
###Getting human dates from model fields
This method works both for string and carbon dates fields.
$model->current_date = '2015-01-01 00:00:00'; echo $model->getHumanDate('current_date'); //January 01, 2015 //Using Carbon datetime format: $format = 'l jS \\of F Y h:i:s A'; echo $model->getHumanDate('current_date', $format); //Thursday 1st of January 01 2015 00:00:00 AM
###Check if an attribute exists in the model
This function verify if an attribute already exists in the current model.
$model = new Model; echo $model->attributeExist('new_property'); //false $model->new_property = null; echo $model->attributeExist('new_property'); //true
###Get changes in a model
Return an array with the affected properties.
$model = new Model; $changes = $model->getChanges(); //array(); $model->client_id = 1; $changes = $model->getChanges(); //array( array('field' => 'client_id', 'old_value' => '', 'new_value' => 1) );
###Create new model from existing using only specific fields Create a new instance only with the fields specified
$model = new Model; $model->client_id = 1; $model->amount = 100; $model->date = Carbon::now(); $new_model_fields = array('client_id', 'amount'); $new_model = $model->extract($new_model_fields); //You are also allowed to change property name: $new_model_fields = array('new_field' => 'client_id', 'amount'); $new_model = $model->extract($new_model_fields);
###Remove model fields Allows you to remove fields in model
$fields_to_remove = array('client_id', 'amount'); $model->removeFields($fields_to_remove);
Using Collection features:
Our model is configured to use our collection which extends from Eloquent Collection, so all methods from the Eloquent Collection can be used.
###Rebuild collection Allows you to rebuild a collection using the fields you want. Imagine you have a user table with the following fields: (id, name, lastname, sex)
$collection = User::all(); //New collection only with the specified fields $format = array('name', 'lastname'); $new_collection = $collection->rebuild($format); //You can also change field names and objects as follow: $format = array('id', 'personal_data' => ['name', 'lastname', 'sex']); $new_collection = $collection->rebuild($format);
###Compare collections Allows you to compare if all values of a field is present in another collection.
$collection = User::all(); $user_avatar_collection = User_avatar::all(); //Check if all users have a record on the user avatar collection $collection->compare($user_avatar_collection, 'user_id', 'id'); //boolean
###Create new instance Allows you to create a new empty instance of the same type of the current collection
$collection = User::all(); $empty_collection = $collection->createNewInstance();
###Get latests rows grouped by fields Return a new collection with the latest rows grouped by the fields specified, in the order of the collection items. Imagine you have a post table with the following fields (id, user_id, post_category_id).
This example allows you to get the latest posts categories for the user.
$collection = Post::all(); $latests = $collection->getLatestsByField( array('user_id', 'post_category_id') );
###Get first rows grouped by fields Return a new collection with the first rows grouped by the fields specified, in the order of the collection items. Using the previous table structure, in this example you get the first posts categories for the user.
$collection = Post::all(); $first = $collection->getFirstByField( array('user_id', 'post_category_id') );
###Sum values by field in collection Sum all values matching the search criteria. In this example the function will sum all products prices from category 10.
$collection = Product::all(); $sum = $collection->sumValues('product_category_id', 10, 'price');
###Find items on collection Allows you to find items on the collection filter by data in the array. In this example we will filter all products with product category 10 and price 100.
$collection = Product::all(); $filter = array('product_category_id' => 10, 'price' => 100); $filtered = $collection->findByFields($filter);
###Merge collections Merge fields from the new collection if values matches. In this example we will merge the avatar file path to the user model.
$users = User::all(); $user_avatar = User_avatar::all() $fields_to_compare = array('id' => 'user_id'); $fields_to_merge = array('file_path'); $users->mergeByFields($user_avatar, $fields_to_compare, $fields_to_merge);
###Custom value for found item Allows you to return a custom value if the item you are looking for it's been found. If no option is specified the model is returned.
$users = User::all(); $filter = array('name' => 'John'); $options = array( 'found_text' => 'Item exist', 'not_found_text' => 'Item not found', 'field' => 'field_name' ); echo $users->showIfFound($filter, $options);
###Delete all models from collection Allows you to delete all models from the database in the current collection.
$user_comments = User_comment::all(); $user_comments->delete();
###Collection average by field Allows you to get the average by a field
$products = Product::all(); echo $products->avg('price'); //Including null values for average, assumed as zero. echo $products->avg('price', true);
###Find items not matching the filter Allows you to find items on the collection not matching the filter criteria. In this example we will filter all products where product category is different to 10.
$collection = Product::all(); $filter = array('product_category_id' => 10); $filtered = $collection->findIfDifferent($filter);
###Get maximum item by field name Get the max value of the given key and return the item. In this example the function will return the max user from the collection.
$users = User::all(); $max_user = $users->maxItem('id');
###Convert collection to flat array Convert the entire collection to a single array using the value of the property specified in the parameter.
$users = User::all(); $flat_users = $users->toFlatArray('id'); var_dump($flat_users); // array(1,2,3)
License
This package is open-sourced software licensed under the MIT license
mookofe/laravel-support 适用场景与选型建议
mookofe/laravel-support 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12 次下载、GitHub Stars 达 3, 最近一次更新时间为 2015 年 07 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「collections」 「package」 「laravel」 「models」 「laravel5」 「Victor Cruz」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mookofe/laravel-support 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mookofe/laravel-support 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mookofe/laravel-support 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This package provides type-safe extension of the laravel collection, forcing a single type of object.
Doctrine Collections adapter for Rekapager pagination library
Library to mimics generic collections
The helpers allows you to easy use DATE. Change format, add/sub day, diff date, get quarter - all this is available in this library.
Simple ASCII output of array data
统计信息
- 总下载量: 12
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-07-19