think.studio / laravel-forms-entries
Package to save forms entries and send notifications.
3.2.0
2023-09-10 11:28 UTC
Requires
- php: ^8.1
- ext-json: *
- laravel/framework: ^9.0|^10.0
- think.studio/laravel-json-field-cast: ^2.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.20
- orchestra/testbench: ^8.5
- phpunit/phpunit: ^10.2
- psalm/plugin-laravel: ^2.8
- vimeo/psalm: ^5.13
README
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>