If you're just doing scaffolding now, you'll need to add @stack('scripts'), @livewireScripts, and @livewireStyles blade directives to your resources/views/layouts/app.blade.php file:
This creates your new form component in the app/Http/Livewire folder.
After making a component, you may want to edit the fields, success, saveAndStayResponse and saveAndGoBackResponse methods:
class UserCreateForm extends FormComponent
{
public function fields()
{
return [
Field::make('Name')->input()->rules('required'),
];
}
public function success()
{
User::create($this->form_data);
}
public function saveAndStayResponse()
{
return redirect()->route('users.create');
}
public function saveAndGoBackResponse()
{
return redirect()->route('users.index');
}
}
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 $fillables via database column names.
Using Form Components
You use form components in views just like any other Livewire component:
@livewire('user-create-form')
Now all you have to do is update your form component class!
Form Component Properties
$model
Optional Eloquent model instance attached to the form component. This is passed in via the @livewire blade directive.
Example:
@livewire('user-edit-form', ['model' => $user])
Example of using the model in the component success method:
public function success()
{
$this->model->update($this->form_data);
}
$form_data
An array of the current data present in the form. This data is keyed with each field name.
Example:
$name = $this->form_data['name'];
$storage_disk
A static property which sets the disk to use for file uploads. Defaults to public.
Example:
private static $storage_disk = 's3';
Or, via .env to apply globally:
FORM_STORAGE_DISK="s3"
$storage_path
A static property which sets the path to use for file uploads. Defaults to uploads.
Example:
private static $storage_path = 'avatars';
Or, via .env to apply globally:
FORM_STORAGE_PATH="avatars"
Form Component Methods
fields()
This method returns an array of Fields to use in the form.
Example:
public function fields()
{
return [
Field::make('Name')->input()->rules('required'),
Field::make('Email')->input('email')->rules(['required', 'email', 'unique:users,email']),
Field::make('Password')->input('password')->rules(['required', 'min:8', 'confirmed']),
Field::make('Confirm Password', 'password_confirmation')->input('password'),
];
}
Sets the field to be an input element. Defaults to text.
$type
Optional HTML5 input type to use for the input.
Example:
Field::make('Email Address')->input('email'),
file()
Sets the field to be a file input element.
File fields should have a nullable text database column, and be cast to array in your model.
This array will be populated with useful info for each file, including file, disk, name, size, and mime_type.
Example migration:
$table->text('photos')->nullable();
Example model casting:
protected $casts = ['photos' => 'array'];
Example field declaration:
Field::make('Photos')->file(),
You can allow multiple file selections using the multiple() method:
Field::make('Photos')->file()->multiple(),
textarea($rows = 2)
Sets the field to be a textarea element.
$rows
The amount of rows to use for the textarea. Defaults to 2.
ArrayFields are slightly different than Fields. They should only be declared within the field array() method.
They have most of the same methods available, except for the file() and array() methods.
They also have a columnWidth() method unavailable to Fields.
make($name)
$name
The name to use for the array field, e.g. phone_number.
Array fields do not use labels. Rather, you should specify a placeholder() for them
请发表评论