michalkortas/laravel-options-creator
Installation:
composer require michalkortas/laravel-options-creator
php artisan vendor:publish --tag=optionscreator --force
This publishes the Blade component and assets to resources/views/vendor/optionscreator/.
Include in Layout:
Add @stack('optionscreator') before </body> in your main layout file (e.g., resources/views/layouts/app.blade.php).
First Use Case:
Wrap a <select> element with the <x-optionscreator-select> component:
<x-optionscreator-select
url="{{ route('store.api.route') }}"
component="components.subEditForm"
>
<select name="mySelect">
<option>Select value</option>
</select>
</x-optionscreator-select>
store.api.route returns JSON data with id and name keys (or adjust successValueKey/successTextKey).component attribute points to a Blade view (e.g., resources/views/components/subEditForm.blade.php) that renders the modal form.Modal Integration:
The package uses Bootstrap Modal for dynamic option creation. Ensure your project includes Bootstrap CSS/JS (or override the modal styles via @stack('optionscreator')).
API Response Handling: The component expects API responses in this format:
{ "id": 1, "name": "Option Name" }
Customize successValueKey (default: id) and successTextKey (default: name) to match your API structure.
Dynamic Form Rendering:
component attribute loads a Blade view (e.g., subEditForm) as the modal content.@inject or session:
@inject('formData', 'App\Services\OptionService')
<x-optionscreator-select
url="{{ route('store.api.route') }}"
component="components.subEditForm"
:formData="$formData->getDefaultValues()"
>
Success/Failure Feedback:
successText and errorText to customize user feedback (supports HTML).errorText (e.g., <strong>Error occurred</strong>. Check console log.).Auto-Select New Option:
Set setNewValue="1" to auto-select the newly created option after submission.
Reusable Modal Forms:
Create a dedicated Blade component for the modal (e.g., resources/views/components/optionCreatorForm.blade.php):
<form id="optionCreatorForm" method="POST" action="{{ $url }}">
@csrf
<input type="text" name="name" placeholder="Option name">
<button type="submit">Save</button>
</form>
Reference it in <x-optionscreator-select>:
<x-optionscreator-select
url="{{ route('store.api.route') }}"
component="components.optionCreatorForm"
>
Dynamic URL Routing: Use route parameters for dynamic endpoints:
<x-optionscreator-select
url="{{ route('store.api.route', ['category' => $categoryId]) }}"
component="components.subEditForm"
>
Conditional Loading:
Disable the component via disabled attribute or hide it with CSS:
<x-optionscreator-select
url="{{ route('store.api.route') }}"
component="components.subEditForm"
disabled="{{ $isReadonly }}"
>
Event Listeners:
Extend functionality by listening to modal events (e.g., show.bs.modal):
document.addEventListener('DOMContentLoaded', function() {
const modal = document.querySelector('[data-bs-target="#optionsCreatorModal"]');
modal.addEventListener('click', function() {
console.log('Modal triggered!');
});
});
Bootstrap Dependency:
@stack('optionscreator').API Response Mismatch:
successValueKey/successTextKey don’t match your API response, the new option won’t appear in the <select>.CSRF Token:
component Blade view includes @csrf:
<form method="POST" action="{{ $url }}">
@csrf
<!-- form fields -->
</form>
Stack Order:
@stack('optionscreator') must be placed before </body> in your layout. Otherwise, the modal JS won’t load.app.blade.php or your main layout.Component Caching:
resources/views/vendor/optionscreator/) may not update after changes.php artisan view:clear
Console Logs:
errorText to log errors to the console (e.g., Check console log.).errorText="<strong>Error!</strong> <script>console.log('API failed:', JSON.stringify({{ json_encode($error) }}))</script>"
Network Tab:
F12 > Network) to verify data flow.Modal Visibility:
<x-optionscreator-select> wrapper is properly closed.Custom Modal Templates: Override the default modal by publishing and modifying the Blade template:
php artisan vendor:publish --tag=optionscreator --force
Edit resources/views/vendor/optionscreator/modal.blade.php.
Event Hooks:
Extend functionality by listening to modal events (e.g., shown.bs.modal):
document.addEventListener('shown.bs.modal', function(e) {
if (e.target.id === 'optionsCreatorModal') {
console.log('Modal shown!');
}
});
Dynamic Attributes:
Pass additional data via data-* attributes on the <select>:
<x-optionscreator-select
url="{{ route('store.api.route') }}"
component="components.subEditForm"
>
<select name="mySelect" data-category-id="{{ $categoryId }}">
<option>Select value</option>
</select>
</x-optionscreator-select>
Access these in your modal component via JavaScript:
const categoryId = document.querySelector('select').dataset.categoryId;
Localization:
Customize text for different languages by overriding the title, saveButtonTitle, etc., attributes dynamically:
<x-optionscreator-select
:title="__('Create new option')"
:saveButtonTitle="__('Save Option')"
url="{{ route('store.api.route') }}"
component="components.subEditForm"
>
How can I help you explore Laravel packages today?