Laravel Useful Additions
A useful collection of additions you may need across multiple projects.
Sometimes you need certain functionality across multiple projects.
Here in our new laravel-useful-additions package we will share from time to time some traits and other functionalities we often use.
To install the package you'll only need to pull it via composer:
1composer require laracraft-tech/laravel-useful-additions
#UsefulEnums
names,values,array
Since PHP 8.1 we finally got enums! These functions can be very handy if you like to** iterate through all of your enum **types or you maybe want to use the enum as an array, for instance in a migration.
1<?php
2
3use LaracraftTech\LaravelUsefulTraits\UsefulEnums;
4
5enum PaymentType: int
6{
7 use UsefulEnums;
8
9 case Pending = 1;
10 case Failed = 2;
11 case Success = 3;
12}
13
14PaymentType::names(); // return ['Pending', 'Failed', 'Success']
15PaymentType::values(); // return [1, 2, 3]
16PaymentType::array(); // return ['Pending' => 1, 'Failed' => 2, 'Success' => 3]
#UsefulScopes
selectAllBut
Select all columns but given excluded array.
1<?php
2
3use LaracraftTech\LaravelUsefulTraits\UsefulScopes;
4
5DB::table('scope_test_table')->insert([
6 'foo' => 'foo',
7 'bar' => 'bar',
8 'quz' => 'quz',
9]);
10
11$class = new class extends Model
12{
13 use UsefulScopes;
14
15 protected $table = 'scope_test_table';
16};
17
18$class::query()->selectAllBut(['foo'])->first()->toArray();
19// return ['bar' => 'bar', 'quz' => 'quz']
Note: Since you** can't **do a native "select all but x,y,z" in mysql, we need to query (and cache) the existing columns of the table, and then exclude the given columns which should be ignored (not selected) by the query.
Cache: The existing column names of each table will be cached** until contents of migrations directory is added or deleted. Modifying the contents of files inside the migrations directory will not re-cache the columns. Consider to clear the cache whenever you make a new deployment/migration**!
fromToday
Select all entries created today.
1<?php
2
3use LaracraftTech\LaravelUsefulTraits\UsefulScopes;
4
5$class = new class extends Model
6{
7 use UsefulScopes;
8
9 protected $timestamps = true;
10 protected $table = 'scope_tests';
11};
12
13$class->create(['foo' => 'foo1', 'bar' => 'bar1', 'quz' => 'quz1']);
14$class->create(['foo' => 'foo2', 'bar' => 'bar2', 'quz' => 'quz2', 'created_at' => now()->yesterday()]);
15
16$class::select('foo')->fromToday()->first()->toArray();
17// return ['foo' => 'foo1']
fromYesterday
Select all entries created yesterday.
1<?php
2
3use LaracraftTech\LaravelUsefulTraits\UsefulScopes;
4
5$class = new class extends Model
6{
7 use UsefulScopes;
8
9 protected $timestamps = true;
10 protected $table = 'scope_tests';
11};
12
13$class->create(['foo' => 'foo1', 'bar' => 'bar1', 'quz' => 'quz1']);
14$class->create(['foo' => 'foo2', 'bar' => 'bar2', 'quz' => 'quz2', 'created_at' => now()->yesterday()]);
15
16$class::select('foo')->fromYesterday()->first()->toArray();
17// return ['foo' => 'foo2']