Resources

Resources tell Lean how to talk to your database layer. Each resource represents one Eloquent model.

These classes are completely static. They don't have any state at all, due to their declarative nature. They only tell Lean what to do, they don't hold any data. So if you add custom methods, make sure they're static.

Creating resources

To create a resource, simply run:

php artisan lean:resource <name>

For example, php artisan lean:resource OrderResource would create an App\Lean\Resources\OrderResource class that uses the App\Models\Order model.

If you wish to change the model, simply use a different class for the $model property.

Defining fields

Fields are the most important part of the resource class. To define them, create a fields() method:

public static function fields(): array
{
    return [
        ID::make('id'),
        Text::make('title')->rules('max:100'),
    ];
}

For a list of available fields and field methods, read the Fields page of the documentation.

Searchable columns

The $searchable property specifies which columns Lean should look at when a user searches on the index action of the resource.

public static array $searchable = [
    'id',
    'name',
    'email',
];

Results per page

To define how many results should be shown per page on index, set the $resultsPerPage property:

public static int $resultsPerPage = 10;

Icon

To specify which icon should be displayed in the menu, set the $icon property:

public static string $icon = 'heroicon-o-user;

You can also set the icon() method if you wish to have more control:

public static function icon(): string
{
    return static::isActive()
        ? 'heroicon-o-lock-open'
        : 'heroicon-o-lock-closed';
}

(Note that the isActive() method reads from the Lean\Lean object's state, as resources themselves are stateless.)

Title

To specify which column should be used as the resource title, set the $title property:

public static string $title = 'name';

Labels

To set singular and plural labels for the resource, use the label() and pluralLabel() methods, respectively. The plural label is displayed in the menu and on the index. The singular label is used for titles and buttons on all the other actions.

public static function label(): string
{
    return __('Order');
}

public static function pluralLabel(): string
{
    return __('Orders');
}

Custom actions

This feature has a dedicated documentation page: Custom Actions

To use a completely custom Livewire component for an action (index/create/show/edit), define a customActions() method:

public static function customActions(): array
{
    return [
        'create' => CreateOrderAction::class,
    ];
}

Custom saving

Although fields let you customize their storing logic, sometimes you may want a bit more control over how resources are saved. For example, you may want to create some other records and emit side effects.

To customize the saving logic completely, define the update() and/or create() methods:

/** @param Product $model */
public static function update($model, array $data)
{
    $shouldUpdateThumbnails = $model->image !== $data['image'];

    $model->update($data);

    if ($shouldUpdateThumbnails) {
        dispatch(new UpdateThumbnails($model));
    }
}

/** @param Product $model */
public static function create($model, array $data)
{
    $model = $model->fill($data);
    $model->save();

    if ($model->image) {
        dispatch(new GenerateThumbnails($model));
    }
}

In the examples above, we simply emit side effects. But we can do much more. For example, we may return completely custom responses:

/** @param Order $model */
public static function create($model, array $data)
{
    // Create the database record
    $model = $model->fill($data);
    $model->save();

    // Redirect to payment page
    return redirect(
        $model->createPaymentIntent()->url()
    );
}

In the example above, we might have a sales person who creates orders for customers on the phone. He creates an Order, saves it, and is redirected to the payment page where he can enter payment details given by the customer on the phone.

Localization

This feature has a dedicated documentation page: Localization

You can customize all language strings used by a resource's actions. For example, you may want to override the create.title language string even if your global lean/resources language string is translated. Some languages make slight changes in words based on the context they're used in.

To specify language strings, set the $lang property:

public static array $lang = [
    'create.title' => 'Vytvořit objednávku',
    'create.submit' => 'Vytvořit objednávku',
    'create.another' => 'Uložit a přidat další objednávku',

    'edit.title' => 'Vytvořit objednávku',
    'edit.submit' => 'Vytvořit objednávku',
];

You may also define a lang() method if you need to use runtime logic (e.g. the __() helper) to generate these strings.

public static function lang(): array
{
    return [
        'create.title' => __('Create order'),
        'create.submit' => __('Create order'),
    ];
}