2017 © Pedro Pelรกez
 

library laravel-macros

A collection of miscellaneous methods to extend some of Laravel's core classes through the use of macros and mixins

image

imliam/laravel-macros

A collection of miscellaneous methods to extend some of Laravel's core classes through the use of macros and mixins

  • Saturday, July 14, 2018
  • by ImLiam
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Laravel Macros

Latest Version on Packagist Total Downloads License, (*1)

A collection of miscellaneous methods to extend some of Laravel's core classes through the use of macros and mixins., (*2)

Installation

You can install the package with Composer using the following command:, (*3)

composer require imliam/laravel-macros:^0.1.0

Usage

Once installed, all macros will automatically be registered and methods will immediately be available for use., (*4)

Illuminate\Support\Collection

Collection@sortByDate($key = null)

Sort the values in a collection by a datetime value., (*5)

To sort a simple list of dates, call the method without passing any arguments to it., (*6)

collect(['2018-01-04', '1995-07-15', '2000-01-01'])->sortByDate();
// return collect(['1995-07-15', '2000-01-01', '2018-01-04'])

To sort a collection where the date is in a specific key, pass the key name when calling the method., (*7)

collect([
    ['date' => '2018-01-04', 'name' => 'Banana'],
    ['date' => '1995-07-15', 'name' => 'Apple'],
    ['date' => '2000-01-01', 'name' => 'Orange']
])->sortByDate('date')
  ->all();

// [
//    ['date' => '1995-07-15', 'name' => 'Apple'],
//    ['date' => '2000-01-01', 'name' => 'Orange'],
//    ['date' => '2018-01-04', 'name' => 'Banana']
// ]

Additionally, you can pass a callback to the method to choose more precisely what is sorted., (*8)

$users = User::all();

$users->sortByDate(function(User $user) {
    return $user->created_at;
})->toArray();

// [
//    ['id' => 12, 'username' => 'spatie', 'created_at' => '1995-07-15'],
//    ['id' => 15, 'username' => 'taylor', 'created_at' => '2000-01-01'],
//    ['id' => 2, 'username' => 'jeffrey', 'created_at' => '2018-01-04']
// ]

Collection@sortByDateDesc($key = null)

This method has the same signature as the sortByDate method, but will sort the collection in the opposite order., (*9)

Collection@keysToValues()

Change the collection so that all values are equal to the corresponding key., (*10)

collect(['a' => 'b', 'c' => 'd'])->keysToValues();
// ['a' => 'a', 'c' => 'c']

Collection@valuesToKeys()

Change the collection so that all keys are equal to their corresponding value., (*11)

collect(['a' => 'b', 'c' => 'd'])->valuesToKeys();
// ['b' => 'b', 'd' => 'd']

Illuminate\Database\Query\Builder

Builder@if($condition, $column, $operator, $value)

Conditionally add where clause to the query builder. See Mohamed Said's blog post for more information., (*12)

Keep chaining methods onto a query being built without having to break it up. Take code like this:, (*13)

$results = DB::table('orders')
    ->where('branch_id', Auth::user()->branch_id);

if($request->customer_id){
    $results->where('customer_id', $request->customer_id);
}

$results = $results->get();

And clean it up into this:, (*14)

$results = DB::table('orders')
    ->where('branch_id', Auth::user()->branch_id)
    ->if($request->customer_id, 'customer_id', '=', $request->customer_id)
    ->get();

Illuminate\Http\Request

Request@replace($key, $value)

Manipulate the request object by replacing a value, or even adding a new one., (*15)

class Middleware
{
    public function handle($request, \Closure $next)
    {
        $request->replace('key', 'value');

        return $next($request);
    }
}

Illuminate\Support\Facades\Route

Route@viewDir($path, $viewDirectory = '', $data = [])

Mimics the functionality offered by Route::view() method but extends it by rerouting requested the URI at any number of sub-levels to match a view directory in the code base., (*16)

This makes it possible to create views with static content and not need to worry about updating routes to match them or using a CMS-style solution to manage them., (*17)

For an example, to see how it works, imagine the following route definition:, (*18)

Route::viewDir('/pages', 'pages');

And the following directory structure for the views:, (*19)

views/
โ”œโ”€โ”€ auth/
โ”œโ”€โ”€ errors/
โ”œโ”€โ”€ layouts/
โ”œโ”€โ”€ pages/
โ”‚   โ”œโ”€โ”€ about-us.blade.php
โ”‚   โ”œโ”€โ”€ faq.blade.php
โ”‚   โ”œโ”€โ”€ privacy-policy.blade.php
โ”‚   โ”œโ”€โ”€ team/
โ”‚   โ”‚   โ”œโ”€โ”€ developers.blade.php
โ”‚   โ”‚   โ”œโ”€โ”€ index.blade.php
โ”‚   โ”‚   โ”œโ”€โ”€ management.blade.php
โ”‚   โ”‚   โ””โ”€โ”€ marketing.blade.php
โ”‚   โ””โ”€โ”€ terms-of-service.blade.php
โ””โ”€โ”€ partials/

The following routes will be generated to match each of the views in the given directory:, (*20)

/pages/about-us
/pages/faq
/pages/privacy-policy
/pages/team
/pages/team/developers
/pages/team/management
/pages/team/marketing
/pages/terms-of-service

Testing

bash composer test, (*21)

Changelog

Please see CHANGELOG for more information on what has changed recently., (*22)

Contributing

Please see CONTRIBUTING for details., (*23)

Security

If you discover any security related issues, please email liam@liamhammett.com instead of using the issue tracker., (*24)

Credits

License

The MIT License (MIT). Please see License File for more information., (*25)

The Versions

14/07 2018

dev-master

9999999-dev https://github.com/imliam/laravel-macros

A collection of miscellaneous methods to extend some of Laravel's core classes through the use of macros and mixins

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

laravel macros mixins laravel-macros imliam

14/07 2018

v0.1.0

0.1.0.0 https://github.com/imliam/laravel-macros

A collection of miscellaneous methods to extend some of Laravel's core classes through the use of macros and mixins

  Sources   Download

MIT

The Requires

  • php ^7.1

 

The Development Requires

laravel macros mixins laravel-macros imliam