Skip to content

Instantly share code, notes, and snippets.

@amberlex78
Last active January 21, 2022 18:39
Show Gist options
  • Save amberlex78/3f5eac3df5fe64d2aa0b8c0ec9e837af to your computer and use it in GitHub Desktop.
Save amberlex78/3f5eac3df5fe64d2aa0b8c0ec9e837af to your computer and use it in GitHub Desktop.
Example BladeServiceProvider with Form::component-s for Laravel with laravelcollective
<?php
class BladeServiceProvider extends ServiceProvider
{
public function boot(): void
{
Form::component('bsText', 'collective.form.text', [
'label', 'name', 'value' => null, 'attributes' => []
]);
Form::component('bsTextarea', 'collective.form.textarea', [
'label', 'name', 'value' => null, 'attributes' => []
]);
}
}
// In the view/collective/form/text.blade.php
<div class="row mb-3">
<label for="{{ $name }}" class="col-sm-2 col-form-label @if(in_array('required', $attributes))required @endif">
{{ $label }}
</label>
<div class="col-sm-10">
{{ Form::text($name, $value, array_merge(['class' => 'form-control'], $attributes)) }}
</div>
</div>
// In the view/collective/form/textarea.blade.php
<div class="row mb-3">
<label for="{{ $name }}" class="col-sm-2 col-form-label @if(in_array('required', $attributes))required @endif">
{{ $label }}
</label>
<div class="col-sm-10">
{{ Form::textarea($name, $value, array_merge(['class' => 'form-control'], $attributes)) }}
</div>
</div>
// Form for creating a new resource.
{{ Form::open(['url' => route('admin.page.store') }}
{{ Form::bsText(__('page.title'), 'title', null, ['required']) }}
{{ Form::bsText(__('page.slug'), 'slug') }}
{{ Form::bsTextarea(__('page.content'), 'content', null, ['required', 'rows' => 3]) }}
{{ Form::close() }}
// CSS for required asterisk
label.required:after {
font-size: 80%;
vertical-align: top;
content: '*';
color: red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment