1. Go to this page and download the library: Download custom-d/laravel-helpers library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
custom-d / laravel-helpers example snippets
class UserPolicy
{
public function viewAny(Authenticatable $user): Response
{
return $user->can('users.viewAny');
}
public function view(Authenticatable $user, User $targetUser): Response
{
return $user->can('users.view');
}
...
namespace App\Models\Policies;
use App\Models\Policies\Traits\CrudPermissions;
class UserPolicy
{
use CrudPermissions;
}
Model::whereNullOrEmpty('column_name'); //generates select * where 1=1 and (column_name is null or column_name = '')
Model::orWhereNullOrEmpty('column_name'); //generates select * where 1=1 or (column_name is null or column_name = '')
Model::whereNotNullOrEmpty('column_name'); //generates select * where 1=1 and (column_name is not null and column_name != '')
Model::orWhereNotNullOrEmpty('column_name'); //generates select * where 1=1 or (column_name is not null and column_name != '')
Model::whereNullOrValue('column_name', [$operator],$value, [$boolean]); to check if column null or specific value (follows laravel where specification where operator is optional)
public function test_external_api()
{
// $this->record = true; // Toggle to create recorded files
$this->processRecordedTest(
'test_external_api',
function () {
// Any HTTP calls made by MyServiceClass will be recorded or returned from recorded responses, depending on `$this->record` above.
$result = resolve(MyServiceClass::class)->execute('foo');
$this->assertEquals('bar', $result->value);
},
'json'
);
}
declare(strict_types=1);
namespace CustomD\LaravelHelpers\Tests\ValueObjects;
use CustomD\LaravelHelpers\ValueObjects\ValueObject;
final readonly class SimpleValue extends ValueObject
{
protected function __construct(
public string $value,
public int $count = 0
) {
}
/** optionally add some validation rules, leave out the method if the type sets are enough **/
public function rules(): array
{
return [
'value' => ['string', 'max:250'],
'count' => ['max:99'],
];
}
}
$simpleValue = SimpleValue::make(value: 'hello World', count: 33);
declare(strict_types=1);
namespace CustomD\LaravelHelpers\Tests\ValueObjects;
use Illuminate\Support\Collection;
use CustomD\LaravelHelpers\ValueObjects\ValueObject;
use CustomD\LaravelHelpers\ValueObjects\Attributes\MakeableObject;
use CustomD\LaravelHelpers\ValueObjects\Attributes\ChildValueObject;
use CustomD\LaravelHelpers\ValueObjects\Attributes\CollectableValue;
use CustomD\LaravelHelpers\ValueObjects\Attributes\MapToCase;
#[MapToCase('camel')]
final readonly class ComplexValue extends ValueObject
{
public function __construct(
#[ChildValueObject(StringValue::class)]
readonly public StringValue $value,
readonly public array $address,
#[ChildValueObject(SimpleValue::class)]
readonly public SimpleValue $simpleValue,
#[MakeableObject(Constructable::class)]
readonly public ?Constructable $constructable = null,
#[CollectableValue(SimpleValue::class)]
readonly ?Collection $simpleValues = null,
) {
}
}
//either in your code where you need it.
$object = ValueObject::fromRequest($MyFormRequest, true|false); //defaults to true (validated only values, false will be all from the request);
//or add a method to your FormRequest
public function getObject(): ValueObject
{
return ValueObject::fromRequest($this);
}
$object->put('key','value'); //will set the key to value
$object->put('key.sub', 'valuesub' ); // will set the array key sub value to valuesub
$object->put('key', ['new' =>'yes']); //will set the array key to be ['new' => 'yes'] -- overwriting the entire array
$object->put(['key.new' => 'yes','key.maybe' => 'no']); //this will NOT override the entire key array, only set the new and maybe values in the actual value object array.
Schema::table('users', function (Blueprint $table) {
$table->string('timezone', 40)->nullable();
});