PHP code example of damianulan / laravel-lucent

1. Go to this page and download the library: Download damianulan/laravel-lucent 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/ */

    

damianulan / laravel-lucent example snippets




namespace App\Services;

use App\Models\Campaign;
use Lucent\Services\Service;

class CreateOrUpdateCampaign extends Service
{
    protected function authorize(): bool
    {
        return $this->request()->user()?->can('campaigns.manage') ?? false;
    }

    protected function rules(): array
    {
        return [
            'name' => [';
    }
}

$service = CreateOrUpdateCampaign::boot(
    request: $request,
    campaign: $campaign,
)->execute();

if (! $service->passed()) {
    return back()->withErrors($service->getErrors());
}

$savedCampaign = $service->getResult();

use App\Forms\BaseForm;
use Lucent\Support\Magellan\MagellanScope;

$forms = MagellanScope::blacklist([
        'App\\Console\\',
        'App\\Providers\\',
    ])
    ->filter(fn (\ReflectionClass $class) => $class->isSubclassOf(BaseForm::class))
    ->get();


namespace App\Support\Magellan;

use Lucent\Support\Magellan\MagellanScope;
use Lucent\Support\Magellan\Workshop\ScopeUsesCache;

class PolicyScope extends MagellanScope implements ScopeUsesCache
{
    protected function scope(\ReflectionClass $class): bool
    {
        return str_ends_with($class->getName(), 'Policy');
    }

    public function ttl(): int
    {
        return 3600;
    }
}

$policies = PolicyScope::get();

use App\Models\Scopes\UserScope;
use Illuminate\Database\Eloquent\Model;
use Lucent\Support\Traits\Accessible;

class User extends Model
{
    use Accessible;

    protected string $accessScope = UserScope::class;
}

$visibleUsers = User::query()->checkAccess()->get();

use Illuminate\Database\Eloquent\Model;
use Lucent\Support\Traits\UUID;

class Order extends Model
{
    use UUID;
}

Schema::create('orders', function (Blueprint $table) {
    $table->uuid('id')->primary();
});

use Illuminate\Database\Eloquent\Model;
use Lucent\Support\Traits\HasUniqueUuid;

class Order extends Model
{
    use HasUniqueUuid;
}

Schema::create('orders', function (Blueprint $table) {
    $table->id();
    $table->uuid('uuid')->unique();
});

$order = Order::findByUuid($uuid);
$uuid = $order->getUuidKey();

public static function getUuidKeyName(): string
{
    return 'public_id';
}

use Illuminate\Database\Eloquent\Model;
use Lucent\Support\Traits\VirginModel;

class Post extends Model
{
    use VirginModel;

    protected $fillable = [
        'title',
        'active',
        'draft',
    ];
}

Post::getAll();
Post::allActive();
Post::allInactive();
Post::allPublished();
Post::allDrafts();

Post::query()->active()->get();
Post::query()->inactive()->get();
Post::query()->published()->get();
Post::query()->drafted()->get();

$post->empty();
$post->notEmpty();

use Illuminate\Database\Eloquent\Model;
use Lucent\Support\Traits\CascadeDeletes;

class User extends Model
{
    use CascadeDeletes;

    protected array $cascadeDelete = ['profile', 'posts'];
}

protected array $donotCascadeDelete = ['auditLogs'];

'models' => [
    'auto_cascade_deletes' => true,
    'cascade_delete_relation_types' => [
        Illuminate\Database\Eloquent\Relations\MorphMany::class,
        Illuminate\Database\Eloquent\Relations\MorphToMany::class,
        Illuminate\Database\Eloquent\Relations\BelongsToMany::class,
        Illuminate\Database\Eloquent\Relations\HasMany::class,
        Illuminate\Database\Eloquent\Relations\HasOne::class,
    ],
]

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Lucent\Support\Traits\SoftDeletesPrunable;

class Archive extends Model
{
    use SoftDeletes;
    use SoftDeletesPrunable;
}

$safe = clean_html($request->input('body'));

clean_html('<script>alert(1)</script>'); // ''
clean_html('<p class="text-center"><strong>Hello</strong></p>');

use App\Models\User;
use Lucent\Support\Traits\Accessible;

if (class_uses_trait(Accessible::class, User::class)) {
    // ...
}

class_uses_trait(string $traitClass, string $targetClass): bool

use Lucent\Support\Str\Alphabet;

Alphabet::getAlphabetPosition('A'); // 1
Alphabet::getAlphabetPosition('É'); // 5
Alphabet::getAlphabetPosition('Ż'); // 26
Alphabet::getAlphabetPosition('ß'); // null

use Lucent\Support\Str\Currencies\CurrencyLib;

$currencies = new CurrencyLib();

$eur = $currencies->getByAlpha3('EUR');
$usd = $currencies->getByCode('840');
$all = $currencies->getAll();

[
    'name' => 'Euro',
    'alpha3' => 'EUR',
    'numeric' => '978',
    'exp' => 2,
    'country' => 'EU',
]

use Lucent\Console\Git;

$branch = Git::head();
$tags = Git::getTags();
$latest = Git::getLatestTagName();

$git = Git::repository(base_path())
    ->queue(['git', 'status', '--short'])
    ->queue(['git', 'rev-parse', '--abbrev-ref', 'HEAD'])
    ->run();

$result = $git->lastResult();

use Lucent\Support\Trace;

$trace = Trace::boot();

$caller = $trace->caller();
$steps = $trace->steps(oldestFirst: true, withSignature: true);
$appFrames = $trace->onlyApplicationFrames()->withoutVendorFrames();
$details = $trace->details();

try {
    // ...
} catch (Throwable $exception) {
    $trace = Trace::fromThrowable($exception);
}

use Illuminate\Support\Facades\Schedule;

Schedule::command('model:prune-soft-deletes')->daily();

return [
    'models' => [
        'prune_soft_deletes_days' => env('PRUNE_SOFT_DELETES_DAYS', 365),
        'auto_cascade_deletes' => true,
        'cascade_delete_relation_types' => [
            Illuminate\Database\Eloquent\Relations\MorphMany::class,
            Illuminate\Database\Eloquent\Relations\MorphToMany::class,
            Illuminate\Database\Eloquent\Relations\BelongsToMany::class,
            Illuminate\Database\Eloquent\Relations\HasMany::class,
            Illuminate\Database\Eloquent\Relations\HasOne::class,
        ],
    ],
    'mews_purifier_setting' => [
        // custom purifier preset used by clean_html()
    ],
    'magellan' => [
        'vendor_
bash
php artisan vendor:publish --tag=lucent
bash
php artisan vendor:publish --tag=lucent-config
php artisan vendor:publish --tag=lucent-langs
bash
php artisan make:service CreateOrUpdateCampaign
bash
php artisan make:magellan AdminFormScope
bash
php artisan make:service PublishArticle
bash
php artisan make:magellan FormScope
bash
php artisan model:prune-soft-deletes