mookofe/laravel-support

为您的当前Laravel模型、集合等提供出色的增强功能。

v1.0.1 2015-09-03 16:10 UTC

This package is not auto-updated.

Last update: 2024-09-18 10:12:44 UTC


README

为您的当前Laravel模型、集合提供出色的增强功能。

Build Status Latest Stable Version License

功能

  • 简单设置
  • 为您的当前模型提供出色的新功能
  • 为您的集合提供新功能

需求

  • illuminate/support: 5.*

版本

1.0.1

安装

准备

打开您的composer.json文件,并将以下内容添加到require数组中

"mookofe/laravel-support": "1.*"

安装依赖

$ php composer install

或者

$ php composer update

集成

更改模型继承,而不是使用默认的Eloquent模型,更改如下

<?php namespace App;

use Mookofe\LaravelSupport\Model;

class User extends Model {

}

使用模型功能

###从模型字段获取人类日期

此方法适用于字符串和carbon日期字段。

    $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

###检查模型中是否存在属性

此函数验证当前模型中是否存在属性。

    $model = new Model;
    echo $model->attributeExist('new_property');          //false
    
    $model->new_property = null;
    echo $model->attributeExist('new_property');          //true

###获取模型中的更改

返回一个包含受影响属性的数组。

    $model = new Model;
    $changes = $model->getChanges();                    //array();
    
    $model->client_id = 1;
    $changes = $model->getChanges();                    //array( array('field' => 'client_id', 'old_value' => '', 'new_value' => 1) );

###从现有模型创建新模型,仅使用特定字段创建仅包含指定字段的新实例

    $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);

###删除模型字段允许您在模型中删除字段

    $fields_to_remove = array('client_id', 'amount');
    $model->removeFields($fields_to_remove);

使用集合功能

我们的模型配置为使用我们的集合,该集合扩展自Eloquent Collection,因此可以像使用Eloquent Collection一样使用所有方法。

###重建集合允许您使用您想要的字段重建集合。想象一下,您有一个用户表,其中包含以下字段:(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);

###比较集合允许您比较另一个集合中是否所有字段的值都存在。

    $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

###创建新实例允许您创建与当前集合相同类型的新空实例

    $collection = User::all();
    $empty_collection = $collection->createNewInstance();

###按字段分组获取最新行返回一个新的集合,其中包含按字段指定的最新行分组,按集合项目的顺序排列。想象一下,您有一个post表,其中包含以下字段(id、user_id、post_category_id)。

此示例允许您获取用户的最新帖子类别。

    $collection = Post::all();
    $latests = $collection->getLatestsByField( array('user_id', 'post_category_id') );

###按字段分组获取第一行返回一个新的集合,其中包含按字段指定的第一行分组,按集合项目的顺序排列。使用之前的表结构,在此示例中,您将获取用户的第一篇帖子类别。

    $collection = Post::all();
    $first = $collection->getFirstByField( array('user_id', 'post_category_id') );

###按字段在集合中求和求匹配搜索标准的所有值的总和。在此示例中,该函数将汇总所有来自类别10的产品价格。

    $collection = Product::all();
    $sum = $collection->sumValues('product_category_id', 10, 'price');

###在集合中查找项目允许您根据数组中的数据在集合中查找项目。在此示例中,我们将筛选所有产品类别为10且价格为100的产品。

    $collection = Product::all();
    $filter = array('product_category_id' => 10, 'price' => 100);
    
    $filtered = $collection->findByFields($filter);

###合并集合如果值匹配,则合并新集合的字段。在此示例中,我们将合并头像文件路径到用户模型。

    $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);

###为找到的项目返回自定义值允许您在找到您要查找的项目时返回自定义值。如果没有指定选项,则返回模型。

    $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);

###从集合中删除所有模型允许您从数据库中删除当前集合中的所有模型。

    $user_comments = User_comment::all();
    $user_comments->delete();

###按字段获取集合的平均值允许您通过字段获取平均值

    $products = Product::all();
    echo $products->avg('price');
    
    //Including null values for average, assumed as zero.
    echo $products->avg('price', true);

###查找不匹配过滤条件的项目允许您在集合中查找不匹配过滤条件的项目。在此示例中,我们将筛选所有产品类别不同于10的产品。

    $collection = Product::all();
    $filter = array('product_category_id' => 10);
    
    $filtered = $collection->findIfDifferent($filter);

###按字段名获取最大项获取给定键的最大值并返回项目。在此示例中,该函数将返回集合中的最大用户。

    $users = User::all();
    $max_user = $users->maxItem('id');

###将集合转换为扁平数组 使用参数中指定的属性值将整个集合转换为单个数组。

    $users = User::all();    
    $flat_users = $users->toFlatArray('id');
    
    var_dump($flat_users);						// array(1,2,3)

许可证

此包是开源软件,受MIT许可证许可。