think.studio/laravel-forms-entries

Package to save forms entries and send notifications.

3.2.0 2023-09-10 11:28 UTC

This package is auto-updated.

Last update: 2024-10-10 14:07:01 UTC


README

Packagist License Packagist Version Total Downloads Build Status Code Coverage Scrutinizer Code Quality

Package to save forms entries (like contact us forms ...) and send notifications

Installation

Install the package via composer:

composer require think.studio/laravel-forms-entries

You can publish the assets file with:

php artisan vendor:publish --provider="FormEntries\ServiceProvider" --tag="config"
php artisan vendor:publish --provider="FormEntries\ServiceProvider" --tag="lang"

To disable default migrations add this code to app service provider:

use FormEntries\Forms\Form;
use FormEntries\Forms\FormContent;

\FormEntries\FormEntryManager::ignoreMigrations()

Form::typesMap([
    'form-contact' => ContactUsForm::class,
]);
FormContent::typesMap([
    'contact-us' => ContactUsFormContent::class,
]);

You can add default routes to your web.php

\FormEntries\Facades\FormEntryManager::routes();

Usage

Use predefined classes

In case you do not need custom classes with validation.

$formEntry = \FormEntries\Forms\UniversalForm::make()
                ->enableStoringData()
                ->enableNotifications()
                ->process($request);

Use custom form and content

// /app/Http/FormEntries/FormContent/ContactUsFormContent.php
class ContactUsFormContent extends FormContent
{
    protected array $requestKeysToSave = ['email', 'message'];

    public function validateRequest(Request $request): static
    {
        $request->validate([
            'email'   => ['required', 'email'],
            'message' => ['required', 'min:10', 'max:500'],
        ]);

        return $this;
    }
}
// /app/Http/FormEntries/Forms/ContactUsForm.php
class ContactUsForm extends Form
{
    protected string $formContentClass = ContactUsFormContent::class;

    public function notify(FormEntry $model): bool
    {
        Notification::route('mail', 'tester@test.admin')
                    ->notify(new ($this->getFormNotificationClass())($model->content));

        return true;
    }
}
<form action="{{route('forms-entries.submit')}}"
      method="post"
>
    @csrf
    <input type="hidden"
           name="{{config('forms-entries.routing.form_name_parameter')}}"
           value="{{\App\Http\FormEntries\Forms\FolioMetricsForm::getType()}}">
    Other fields
    <button type="submit">Submit</button>
</form>

Credits

  • Think Studio