lyhty/macros is a Laravel package for helpful macros for your laravel project..
It currently has 0 GitHub stars and 180 downloads on Packagist (latest version v7.1.3).
Install it with composer require lyhty/macros.
Discover more Laravel packages by lyhty
or browse all Laravel packages to compare alternatives.
Last updated
This package provides some additional, convenient macros for you to use with your Laravel project.
Install the package with Composer:
composer require lyhty/macros
The package registers itself automatically.
Here's a brief documentation on the macros the package provides.
Illuminate\Database\Eloquent\Builder
Illuminate\Database\Query\Builder
Illuminate\Support\Collection
Illuminate\Support\Arr
Illuminate\Support\Str
Illuminate\Support\Stringable
Carbon\CarbonPeriod
Illuminate\Database\Eloquent\BuilderBuilder::selectKeySelect the key of the model in the query (uses Model's getKey method).
$query = User::query()->selectKey();
$query->toSql(); // "select `id` from `users`"
Builder::searchLike & orSearchLike⚠️ This macro relies on
Str::explodeReversemacro. If you want to disable that macro, this macro will no longer function.
⚠️ The
Builder::orSearchLikemacro relies onBuilder::searchLikemacro. If you want to disable thesearchLikemacro, be sure to disable theorSearchLikemacro as well.
use Lyhty\Macros\SearchPattern;
$query = User::query()
->searchLike('name', 'Matti Suo', SearchPattern::Left)
->orSearchLike('name', 'ranie', SearchPattern::Both)
->orSearchLike('name', 'mi', SearchPattern::Right)
->orSearchLike('name', 'Mat%i%emi', SearchPattern::Custom);
$query->toSql();
// "select * from `users` where (`users`.`name` LIKE ?) or (`users`.`name` LIKE ?) or (`users`.`name` LIKE ?) or (`users`.`name` LIKE ?)"
// First ? being "Matti Suo%", second "%ranie%", third "%mi" and fourth being the given search pattern as the search term ("Mat%i%emi" in this case)
$query = User::query()->searchLike('games.name', 'Apex Leg', SearchPattern::Right);
$query->toSql();
// select * from `users` where (exists
// (select * from `games` where `users`.`id` = `games`.`user_id` and `games`.`name` LIKE ?)
// )
// ? being "Apex Leg%"
// The pattern defaults to SearchPattern::Both, which means the given string will be wrapped with '%'.
$query = User::query()->searchLike(['games.console.name', 'games.platforms.name'], 'Xbox');
$query->toSql();
// select * from `users` where (exists (select * from `games` where `users`.`id` = `games`.`user_id` and (exists
// (select * from `consoles` where `games`.`console_id` = `consoles`.`id` and (`consoles`.`name` LIKE ?)) or exists
// (select * from `platforms` inner join `platform_game` on `platforms`.`id` = `platform_game`.`platform_id` where
// `games`.`id` = `platform_game`.`game_id` and (`platforms`.`name` LIKE ?)))))
// ? being "Xbox"
Illuminate\Database\Query\BuilderBuilder::selectRawArrAdd 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" => "[email protected]"]
// Instead of:
$query = User::query()->selectRaw('concat(`id`, "-", `name`) as id_name, concat(`email`, "-", `name`) as email_name');
// 🤢
Illuminate\Support\CollectionCollection::mergeManyMerge 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]
Collection::pick (was pluckMany)Pick several keys from the collection items. The first value should be an array of keys you want to pick up from the collection items. The second value determines whether keys will be preserved and in which format:
PICK_WITH_FULL_KEYS (>= 2):
PICK_WITH_PARTIAL_KEYS (1):
PICK_WITHOUT_KEYS (0):
$data = User::query()->get();
$data->pick(['id', 'name', 'metadata.loggedIn'])->toArray();
// [[1, "Matti Suoraniemi", true], [2, "Foo Bar", false]]
$data->pick(['id', 'name', 'metadata.loggedIn'], 1)->toArray();
// [
// ["id" => 1, "name" => "Matti Suoraniemi", "loggedIn" => true],
// ["id" => 2, "name" => "Foo Bar", "loggedIn" => false]
// ]
$data->pick(['id', 'name', 'metadata.loggedIn'], 2)->toArray();
// [
// ["id" => 1, "name" => "Matti Suoraniemi", "metadata" => ["loggedIn" => true]],
// ["id" => 2, "name" => "Foo Bar", "metadata" => ["loggedIn" => false]]
// ]
Collection::whereExtendsFilter 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
Collection::whereImplementsFilter 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"]
Collection::whereUsesFilter 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\ArrArr::associateConverts 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" => []]
Arr::combineSimilar 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]
Arr::fillKeysFills 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]
Arr::implodeImplodes given array with given separator to a \Illuminate\Support\Stringable instance.
$array = ['foo', 'bar'];
(string) Arr::implode($array, ' ')->upper(); // "FOO BAR"
Arr::zipZips the key and value together with the given zipper.
Arr::zip(['foo' => 'bar', 'zoo' => 'gar'], ':'); // ["foo:bar", "zoo:gar"]
Arr::unzipUnzips keys to key and value with the given zipper.
Arr::unzip(['foo:bar', 'zoo:gar'], ':'); // ["foo" => "bar", "zoo" => "gar"]
Illuminate\Support\StrStr::explodeReverseExplodes the given string from the end instead of the start and returns it as
a Illuminate\Support\Collection class instance.
Str::explodeReverse('games.platforms.name', '.', 2)->toArray(); // ['games.platforms', 'name']
// Whereas normal explode function would do:
explode('.', 'games.platforms.name', 2); // ['games', 'platforms.name']
Str::wrapWithWraps the string with given character(s).
Str::wrapWith('foo', ':'); // ":foo:"
Str::wrapWith('bar', '<', '>'); // "<bar>"
Str::wrapWith('!zoo', '!'); // "!zoo!"
⚠️ As Laravel 9 introduced
Str::wrapmacro, as of v4.0 this macro is now calledStr::wrapWithto avoid conflicts. Note: the behavior between these two macros is different:
Str::wrapWith(':foo', ':'); // ":foo:"
Str::wrap(':foo', ':'); // "::foo:"
Illuminate\Support\StringableStringable::explodeReverse⚠️ This macro relies on
Str::explodeReversemacro. If you want to disable that macro, this macro will no longer function.
See Illuminate\Support\Str::explodeReverse
Str::of('games.platforms.name')->explodeReverse('.', 2)->toArray(); // ['games.platforms', 'name']
// Whereas normal explode function would do:
Str::of('games.platforms.name')->explode('.', 2)->toArray(); // ['games', 'platforms.name']
Stringable::wrapWith⚠️ As Laravel 9 introduced
Str::wrapmacro, as of v4.0 this macro is now calledStr::wrapWithto avoid conflicts.
⚠️ This macro relies on
Str::wrapWithmacro. If you want to disable that macro, this macro will no longer function.
See Illuminate\Support\Str::wrapWith
(string) Str::of('foo')->upper()->wrapWith(':'); // ":FOO:"
(string) Str::of('bar')->upper()->wrapWith('<', '>'); // "<BAR>"
(string) Str::of('!zoo')->upper()->wrapWith('!'); // "!ZOO!"
Carbon\CarbonPeriodCarbonPeriod::collect$dates = CarbonPeriod::between('yesterday', 'today')->collect();
$dates->first()->toDateTimeString(); // "2022-06-14 00:00:00"
Lyhty Macros is open-sourced software licensed under the MIT license.