-
-
Notifications
You must be signed in to change notification settings - Fork 86
Create Form
- Easy: the artisan command, generate form
- Blazing fast: sponsor artisan command, auto generate forms, for all models, WITH fields
- Manual: add the trait OR extend the component
This command will create a new form component in app/Http/Livewire/Forms
folder.
php artisan make:tall-form CreateUser --model=User
php artisan make:tall-form {name} {--model=Model} {--path=Http/Livewire/Forms} {--modelspath=Models} {--action=create} {--overwrite=false} {--skipexisting=false}
Options, and their default values.
-
--path =
App/Http/Livewire/Forms
Output path. -
--modelspath =
Models
. You can set this toApp
orAny\\Path\\With\\Backslash
/Or/Slash
. -
--action =
create
. Options:create
oredit
. Controls which stub to be used -
--overwrite =
false
. WARNING: Overwrites ALL existing forms, without prompts. -
--skipexisting =
false
.If false && overwrite=false
, you'll be prompted to confirm overwriting EACH existing file.
- There are two stubs. One for
create
forms and one forupdate
forms. - Defined by the
--action
parameter in the make command. - The
--action=create
stub is suitable for forms with optional route model binding.
Read more about route model binding ->
Create a component in the Controllers directory
php artisan make:tall-form TestCommand --model=User --path=Http/Controllers/Livewire/Forms
Use a model in the App
directory, example: use App\User;
php artisan make:tall-form TestCommand --model=User --modelspath="App"
Auto generate forms, for all Eloquent models in your project, with fields for all db columns - using a single artisan command.
Please 💗 sponsor this package 🔗 to get access to this command.
Watch the demo on YouTube: https://www.youtube.com/watch?v=i4dVmFubXFs
php artisan make:tall-forms-for-all-models
The Artisan make command adds the TallForm
trait to your Livewire Component.
use Tanthammar\TallForms\TallForm;
use Livewire\Component;
class SomeComponent extends Component
{
use TallForm;
}
For use cases where you want to avoid Trait
method naming, collisions
use Tanthammar\TallForms\TallFormComponent;
class SomeComponent extends TallFormComponent
{
//
}
After making a component, you may want to edit the fields
, onCreateModel
, onUpdateModel
, and formTitle
methods:
namespace App\Http\Livewire\Forms;
use App\Models\User;
use Livewire\Component;
use Tanthammar\TallForms\Input;
use Tanthammar\TallForms\TallForm;
class CreateUser extends Component
{
use TallForm;
public function mount(?User $user)
{
//Gate::authorize()
$this->fill([
'formTitle' => trans('global.create').' '.trans('crud.user.title_singular'),
'wrapWithView' => true,
'showGoBack' => false,
]);
$this->mount_form($user); // $user from hereon, called $this->model
}
public function onCreateModel($validated_data)
{
// Set the $model property in order to conditionally display fields when the model instance exists, on saveAndStayResponse()
$this->model = User::create($validated_data);
}
// OPTIONAL method used for the "Save and stay" button, this method already exists in the TallForm trait
public function onUpdateModel($validated_data)
{
$this->model->update($validated_data);
}
public function fields()
{
return [
Input::make('Name')->rules('required'),
];
}
}
You don't have to use the render()
method in your form component or worry about a component view, because the package handles that automatically.
Protip: you can add the FillsColumns
trait to your model for automatic $fillable
s via database column names.
The model you pass in the mount_form($model)
method is accessed as $this->model
You use form components in views just like any other Livewire component.
Don't forget to prefix the component name with the --path
you defined
in the artisan make:tall-form
command.
With the default path
set to App/Http/Livewire/Forms
, it would look like this:
<livewire:forms.create-user :user="$user"/>
See the Model binding wiki page
Route::get('/user/{user?}', \App\Http\Livewire\Forms\CreateUser::class);
- Installation
- Requirements
- v5 Upgrade Guide
- v6 Upgrade Guide
- v7 Upgrade Guide
- Support
- Quickstart
- Manual installation
- Optional
- Form component
- Field
- Field types
- Example Form
- Blade Components
- Notifications