PHP code example of spaanproductions / laravel-form
1. Go to this page and download the library: Download spaanproductions/laravel-form library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use SpaanProductions\LaravelForm\Eloquent\FormAccessible;
class User extends Model
{
use FormAccessible;
protected $fillable = ['name', 'email', 'birth_date'];
/**
* Form mutator for birth_date
*/
public function formBirthDateAttribute($value)
{
return $value ? $value->format('Y-m-d') : null;
}
/**
* Form mutator for full_name (computed field)
*/
public function formFullNameAttribute($value)
{
return $this->first_name . ' ' . $this->last_name;
}
}
// In your controller
public function edit(User $user)
{
return view('users.edit', compact('user'));
}
// In your Blade template
{{ Form::model($user, ['route' => ['users.update', $user->id], 'method' => 'PUT']) }}
{{ Form::text('name') }}
{{ Form::email('email') }}
{{ Form::date('birth_date') }}
{{ Form::text('full_name') }} {{-- Uses form mutator --}}
{{ Form::submit('Update User') }}
{{ Form::close() }}
// Register a component in your service provider
Form::component('bsText', 'components.form.text', ['name', 'value', 'attributes']);
// Create the component view: resources/views/components/form/text.blade.php
<div class="form-group">
<label for="{{ $name }}">{{ ucfirst($name) }}</label>
<input type="text"
name="{{ $name }}"
value="{{ $value }}"
class="form-control {{ $attributes['class'] ?? '' }}"
{{ $attributes['
// Register a component
Html::component('alert', 'components.alert', ['type', 'message']);
// Create the component view: resources/views/components/alert.blade.php
<div class="alert alert-{{ $type }}">
{{ $message }}
</div>
// Use the component
{{ Html::alert('success', 'Operation completed successfully!') }}
{{ Form::open(['route' => 'users.store']) }}
{{ Form::considerRequest() }}
{{ Form::text('name') }} {{-- Will be filled from request if available --}}
{{ Form::email('email') }}
{{ Form::submit('Create User') }}
{{ Form::close() }}
{{ Form::text('name', null, ['class' => 'form-control']) }}
{{ Form::email('email', null, ['class' => 'form-control']) }}
// If validation fails, the fields will be automatically filled with old input
{{ Form::open(['route' => ['users.update', $user->id], 'method' => 'PUT']) }}
{{ Form::text('name') }}
{{ Form::submit('Update') }}
{{ Form::close() }}
// This will create a form with method="POST" and add a hidden _method field with value "PUT"