dixipro/magicpro
Laravel package that helps integrate MagicPro features into your app, providing utilities and components to speed up development and simplify common tasks. Intended for quick setup and cleaner code when adding MagicPro-related functionality.
Installation
composer require dixipro/magicpro
php artisan vendor:publish --provider="Dixipro\Magicpro\MagicproServiceProvider" --tag="magicpro-config"
Define a Form Schema
Create a config file at config/magicpro/forms/your_form.php:
return [
'title' => 'Contact Us',
'fields' => [
'name' => [
'type' => 'text',
'label' => 'Your Name',
'rules' => 'required|string|max:255',
],
'email' => [
'type' => 'email',
'label' => 'Email',
'rules' => 'required|email',
],
],
'submit_button' => 'Send Message',
];
First Use Case: Render a Form In a Blade template:
@php
$form = \Dixipro\Magicpro\Facades\Magicpro::form('your_form');
@endphp
{!! $form->render() !!}
Handle Submissions In your controller:
use Dixipro\Magicpro\Facades\Magicpro;
public function store(Request $request)
{
$form = Magicpro::form('your_form');
$validated = $form->validate($request->all());
if ($form->fails()) {
return back()->withErrors($form->errors());
}
// Process data (e.g., save to DB)
$form->save();
return redirect()->route('thank-you');
}
Route the Form
In routes/web.php:
Route::get('/contact', [ContactController::class, 'create']);
Route::post('/contact', [ContactController::class, 'store']);
config/magicpro.php (default settings, field types, storage)\Dixipro\Magicpro\Facades\Magicpro (core methods like form(), validate(), save())config/magicpro/field_types.php (extendable)FormSubmitted, FormValidated (for custom logic)// config/magicpro/forms/user_profile.php
return [
'fields' => [
'bio' => [
'type' => 'textarea',
'label' => 'About You',
'rules' => 'nullable|max:500',
'hint' => 'Tell us about yourself',
],
'avatar' => [
'type' => 'file',
'label' => 'Profile Picture',
'rules' => 'nullable|image|mimes:jpeg,png|max:2048',
],
],
'conditional' => [
'avatar' => ['show' => ['bio' => '!=', '']], // Show if bio is not empty
],
];
config() helper to load schemas dynamically:
$schema = config("magicpro.forms.{$formName}");
$form = Magicpro::form($formName)->setSchema($schema);
// In Blade: Loop through fields dynamically
@foreach($form->fields() as $name => $field)
<div class="form-group">
<label for="{{ $name }}">{{ $field['label'] }}</label>
{!! $form->field($name)->render() !!}
@if($errors->has($name))
<span class="text-red-500">{{ $errors->first($name) }}</span>
@endif
</div>
@endforeach
$form->field($name) to render individual fields.$form->getField($name)['rules'].public function update(Request $request, $id)
{
$form = Magicpro::form('user_profile');
$validated = $form->validate($request->all());
if ($form->fails()) {
return back()
->withInput()
->withErrors($form->errors());
}
// Proceed with update
$user->update($validated);
}
$form->addRule('custom_field', 'custom_rule');
// In schema
'conditional' => [
'address' => ['show' => ['country' => '!=', 'US']],
'tax_id' => ['show' => ['business_type' => '=', 'llc']],
],
if ($form->shouldShow('address')) {
$form->field('address')->render();
}
// Save to a model
$form->save(new User());
// Or use a custom storage handler
$form->setStorageHandler(function ($data) {
// Custom logic (e.g., API call, database insert)
});
event(new FormSubmitted($form));
// Controller
public function getFormSchema($formName)
{
return response()->json(Magicpro::form($formName)->getSchema());
}
public function submitForm(Request $request)
{
$form = Magicpro::form($request->form_name);
$validated = $form->validate($request->all());
if ($form->fails()) {
return response()->json(['errors' => $form->errors()], 422);
}
$form->save();
return response()->json(['success' => true]);
}
fetch() to load schemas dynamically:
async function loadForm(formName) {
const response = await fetch(`/api/forms/${formName}/schema`);
const schema = await response.json();
renderForm(schema);
}
// Step 1: Personal Info
$form = Magicpro::form('multi_step')->step('personal');
$form->render();
// Step 2: Address (next request)
$form = Magicpro::form('multi_step')->step('address');
$form->validate($request->all());
session() to store step progress:
session()->put('form_progress', ['step' => 'address']);
// In a service provider
Magicpro::extend(function ($form) {
$form->onSubmit(function ($data) {
// Send to Slack
Http::post('https://slack.com/api/chat.postMessage', [
'text' => "New form submission: " . json_encode($data),
]);
});
});
unique:users|email) in schemas.if (!Gate::allows('view-form', $formName)) {
abort(403);
}
'label' => __('forms.contact.name'),
$response = $this->post('/contact', ['name' => 'Test']);
$response->assertSessionHasNoErrors();
@include('magicpro::form') for default rendering.<div x-data="{ open: false }">
<button @click
How can I help you explore Laravel packages today?