PHPackages                             sirmathays/convenient-laravel-macros - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sirmathays/convenient-laravel-macros

Abandoned → [lyhty/macros](/?search=lyhty%2Fmacros)ArchivedLibrary[Utility &amp; Helpers](/categories/utility)

sirmathays/convenient-laravel-macros
====================================

Helpful macros for your Laravel project.

v1.3.2(3y ago)015MITPHPPHP ^7.4|^8.0

Since Jun 14Pushed 3y ago1 watchersCompare

[ Source](https://github.com/sirmathays/convenient-laravel-macros)[ Packagist](https://packagist.org/packages/sirmathays/convenient-laravel-macros)[ Docs](https://matti.suoraniemi.com/creations/packages/convenient-laravel-macros)[ RSS](/packages/sirmathays-convenient-laravel-macros/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (7)Used By (0)

 [![](https://camo.githubusercontent.com/b1ca2162dc83c1c8c08ef0b387d0d33ced0d8972d28945170f4b4377426d70e9/68747470733a2f2f6d617474692e73756f72616e69656d692e636f6d2f73746f726167652f666f727572332e706e67)](https://camo.githubusercontent.com/b1ca2162dc83c1c8c08ef0b387d0d33ced0d8972d28945170f4b4377426d70e9/68747470733a2f2f6d617474692e73756f72616e69656d692e636f6d2f73746f726167652f666f727572332e706e67)

 [ ![Total Downloads](https://camo.githubusercontent.com/a91b798463b40d474d9ad569aba3bf912b4d334a4bed3e5ac31b86f0fa0581f7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7369726d6174686179732f636f6e76656e69656e742d6c61726176656c2d6d6163726f73) ](https://packagist.org/packages/sirmathays/convenient-laravel-macros) [ ![Latest Stable Version](https://camo.githubusercontent.com/c42401f0870c4f77e3a82344a7d03c9303410c189b22c5c06d4e586097d1d5a6/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7369726d6174686179732f636f6e76656e69656e742d6c61726176656c2d6d6163726f73) ](https://packagist.org/packages/sirmathays/convenient-laravel-macros) [ ![License](https://camo.githubusercontent.com/f32e809e7a0c28fb44bd64244de0dd1797f0460377a0941f9782faada3baae9a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f7369726d6174686179732f636f6e76656e69656e742d6c61726176656c2d6d6163726f73) ](https://packagist.org/packages/sirmathays/convenient-laravel-macros)

This package provides some additional, convenient macros for you to use with your Laravel project.

Installation
------------

[](#installation)

Install the package with Composer:

```
composer require sirmathays/convenient-laravel-macros

```

The package registers itself automatically.

Macros
------

[](#macros)

Here's a brief documentation on the macros the package provides.

- `Illuminate\Database\Eloquent\Builder`
    - [`selectKey`](#illuminatedatabaseeloquentbuilderselectkey)
    - [`whereLike` &amp; `orWhereLike`](#illuminatedatabaseeloquentbuilderwherelike--orwherelike)
- `Illuminate\Database\Query\Builder`
    - [`selectRawArr`](#illuminatedatabasequerybuilderselectrawarr)
- `Illuminate\Support\Collection`
    - [`mergeMany`](#illuminatesupportcollectionmergemany)
    - [`pluckMany`](#illuminatesupportcollectionpluckmany)
    - [`whereExtends`](#illuminatesupportcollectionwhereextends)
    - [`whereImplements`](#illuminatesupportcollectionwhereimplements)
    - [`whereUses`](#illuminatesupportcollectionwhereuses)
- `Illuminate\Support\Arr`
    - [`combine`](#illuminatesupportarrcombine)
    - [`fillKeys`](#illuminatesupportarrfillkeys)
    - [`join`](#illuminatesupportarrjoin)
    - [`zip`](#illuminatesupportarrzip)
    - [`unzip`](#illuminatesupportarrunzip)
- `Illuminate\Support\Str`
    - [`wrap`](#illuminatesupportstrwrap)
- `Illuminate\Support\Stringable`
    - [`wrap`](#illuminatesupportstringablewrap)
- `Carbon\CarbonPeriod`
    - [`collect`](#carboncarbonperiodcollect)

### `Illuminate\Database\Eloquent\Builder::selectKey`

[](#illuminatedatabaseeloquentbuilderselectkey)

Select the key of the model in the query (uses Model's `getKey` method).

```
$query = User::query()->selectKey();

$query->toSql() // "select `id` from `users`"
```

### `Illuminate\Database\Eloquent\Builder::whereLike` &amp; `orWhereLike`

[](#illuminatedatabaseeloquentbuilderwherelike--orwherelike)

```
$query = User::query()
    ->whereLike('name', 'Matti Suo', 'right')
    ->orWhereLike('name', 'ranie');
    ->orWhereLike('name', 'mi', 'left');

$query->toSql(); // "select * from `users` where (`name` LIKE ?) or (`name` LIKE ?) or (`name` LIKE ?)"
// First ? being "Matti Suo%", second "%ranie%" and third "%mi"

$query = User::query()->whereLike('games.name', 'Apex Leg', 'right');

$query->toSql();
// select * from `users` where (exists (select * from `games` where `users`.`id` = `games`.`user_id` and `name` LIKE ?))
// ? being "Apex Leg%"
```

### `Illuminate\Database\Query\Builder::selectRawArr`

[](#illuminatedatabasequerybuilderselectrawarr)

Add raw select statements as an array, instead of as a one ugly string (`selectRaw`).

```
$query = User::query()->selectRawArr([
    'concat(`id`, "-", `name`) as id_name'
    'concat(`email`, "-", `name`) as email_name'
]);

$query->first()->toArray() // ["id_name" => "1-Matti", "email_name" => "matti@suoraniemi.com-Matti"]

// VS

$query = User::query()->selectRaw('concat(`id`, "-", `name`) as id_name, concat(`email`, "-", `name`) as email_name');
```

### `Illuminate\Support\Collection::mergeMany`

[](#illuminatesupportcollectionmergemany)

Merge multiple arrays/collections to the collection in one go.

```
$data = new Collection([1,2,3]);

$data->mergeMany([4], [5], [6]); // [1, 2, 3, 4, 5, 6]
```

### `Illuminate\Support\Collection::pluckMany`

[](#illuminatesupportcollectionpluckmany)

Pluck several keys from the collection items.

```
$data = User::query()->get();

$data->pluckMany('id', 'name')->toArray(); // [["id" => 1, "name" => "Matti Suoraniemi"]]
```

### `Illuminate\Support\Collection::whereExtends`

[](#illuminatesupportcollectionwhereextends)

Filter classes and/or objects that extend the given class.

```
use Illuminate\Database\Eloquent\Model;

$data = new Collection([
    \App\Models\User::class,
    \App\Models\Game::class,
    \App\Models\Console::class,
    \App\Models\Hobby::class,
]);

$data->whereExtends(Model::class)->count(); // 4
```

### `Illuminate\Support\Collection::whereImplements`

[](#illuminatesupportcollectionwhereimplements)

Filter classes and/or objects that implement the given interface.

```
use App\Contracts\PlayableOnConsole;

$data = new Collection([
    \App\Models\User::class,
    \App\Models\Game::class,
    \App\Models\Console::class,
    \App\Models\Hobby::class,
]);

$data->whereImplements(PlayableOnConsole::class)->toArray(); // ["App\Models\Game"]
```

### `Illuminate\Support\Collection::whereUses`

[](#illuminatesupportcollectionwhereuses)

Filter classes and/or objects that use the given trait.

```
use Illuminate\Notifications\Notifiable;

$data = new Collection([
    \App\Models\User::class,
    \App\Models\Game::class,
    \App\Models\Console::class,
    \App\Models\Hobby::class,
]);

$data->whereUses(Notifiable::class)->toArray(); // ["App\Models\User"]
```

### `Illuminate\Support\Arr::associate`

[](#illuminatesupportarrassociate)

Converts an array into a fully associative array by converting any values with an integer key to the value being the key filled with the given fill value. Values that have a string key already won't be touched.

```
Arr::associate(['foo']) // ["foo" => null]
Arr::associate(['foo', 'bar' => []], []) // ["foo" => [], "bar" => []]
Arr::associate(['foo', 'bar' => []], fn () => Arr::random(['foo', 'bar'])) // ["foo" => "foo", "bar" => []]
Arr::associate([fn () => Str::reverse('foo'), 'bar' => []]) // ["oof" => null, "bar" => []]
```

### `Illuminate\Support\Arr::combine`

[](#illuminatesupportarrcombine)

Similar to `array_combine`, but allows to have more keys than values. Keys without value will be set as null.

```
Arr::combine(['foo', 'zoo'], ["bar", "gar"]) // ["foo" => "bar", "zoo" => "gar"]
Arr::combine(['foo', 'zoo'], ["bar"]) // ["foo" => "bar", "zoo" => null]
```

### `Illuminate\Support\Arr::fillKeys`

[](#illuminatesupportarrfillkeys)

Fills given keys with given value. You can also set that only keys that already exist in the array can become filled. In other words, if the key `foo` is to be filled with value `bar`, but the key `foo` doesn't already exist in the array, the array will remain unchanged.

```
$array = ['foo' => 'bar', 'zoo' => 'gar'];

Arr::fillKeys($array, ['foo', 'zoo'], null) // ["foo" => null, "zoo" => null]
Arr::fillKeys($array, ['foo', 'zoo', 'boo'], null) // ["foo" => null, "zoo" => null, "boo" => null]
Arr::fillKeys($array, ['foo', 'zoo', 'boo'], null, true) // ["foo" => null, "zoo" => null]
```

### `Illuminate\Support\Arr::join`

[](#illuminatesupportarrjoin)

Collection's nice join method brought to Arr.

```
Arr::join(['foo', 'bar', 'zoo'], ', ', ' and ') // "foo, bar and zoo"
```

### `Illuminate\Support\Arr::zip`

[](#illuminatesupportarrzip)

Zips the key and value together with the given zipper.

```
Arr::zip(['foo' => 'bar', 'zoo' => 'gar'], ':') // ["foo:bar", "zoo:gar"]
```

### `Illuminate\Support\Arr::unzip`

[](#illuminatesupportarrunzip)

Unzips keys to key and value with the given zipper.

```
Arr::unzip(['foo:bar', 'zoo:gar'], ':') // ["foo" => "bar", "zoo" => "gar"]
```

### `Illuminate\Support\Str::wrap`

[](#illuminatesupportstrwrap)

Wraps the string with given character(s).

```
Str::wrap('foo', ':') // ":foo:"
Str::wrap('bar', '') // ""
Str::wrap('!zoo', '!') // "!zoo!"
```

### `Illuminate\Support\Stringable::wrap`

[](#illuminatesupportstringablewrap)

Wraps the string with given character(s).

```
(string) Str::of('foo')->upper()->wrap(':') // ":FOO:"
(string) Str::of('bar')->upper()->wrap('') // ""
(string) Str::of('!zoo')->upper()->wrap('!') // "!ZOO!"
```

### `Carbon\CarbonPeriod::collect`

[](#carboncarbonperiodcollect)

```
$dates = CarbonPeriod::between('yesterday', 'today')->collect();

$dates->first()->toDateTimeString() // "2022-06-14 00:00:00"
```

Extending the MacroServiceProvider
----------------------------------

[](#extending-the-macroserviceprovider)

You can make your own pretty MacroServiceProviders by extending the MacroServiceProvider class provided by this package!

Here's an example:

```
