PHP code example of gsferro / select2easy
1. Go to this page and download the library: Download gsferro/select2easy 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/ */
gsferro / select2easy example snippets
public static function sl2Name(string $term, int $page, string $parentId)
{
$select2Search = [
"name",
];
$select2Text = "name";
$scopeParentAndId = [
'parent' => $parentId,
];
return self::select2easy(
$term,
$page,
$select2Search,
$select2Text,
$limitPage = 6,
$extraScopes = [],
$prefix = null,
$scopeParentAndId,
$suffix = null,
);
}
public static function sl2Name(string $term, int $page, string $parentId)
{
$select2Search = [
"name",
];
$select2Text = "name";
$scopeParentAndId = [
'parent' => $parentId,
];
return self::select2easy(
term: $term,
page: $page,
select2Search: $select2Search,
select2Text: $select2Text,
scopeParentAndId: $scopeParentAndId
);
}
public static function sl2Name(string $term, int $page, string $parentId)
{
$select2Search = [
"name",
];
$select2Text = "name";
$markups = [
'text' => 'sl2MarkupText',
'html' => 'sl2MarkupHtml',
];
return self::select2easy(
term: $term,
page: $page,
select2Search: $select2Search,
select2Text: $select2Text,
markups: $markups
);
}
# Pode ser usando assim: return string
private function sl2MarkupHtml(string $text, Country $model): string
{
return '<span class="select2-selection__rendered" id="select2_country-container"
role="textbox" aria-readonly="true" title="'.$text.'">
<span>
<img src="'.$model->image.'" class="rounded-circle" alt="image">
'.$model->name.'
</span>
</span>';
}
# Ou, pode ser usando assim: return view renderizada
private function sl2MarkupText(string $text, Country $model): string
{
return view('country', [
'text' => $text,
'model' => $model
])->render();
}
@props([
'label',
'isRequired' => false,
])
<label {{ $attributes->merge([ 'class' => 'form-label' ])->whereDoesntStartWith('label') }}>
{{ $label }} {{ $isRequired ? '*' : '' }}
</label>
@props([
'name',
'groupClass',
'appModel',
'col' => 12,
'colMd' => 4,
'id' => null,
'sl2' => 'sl2Name',
'label' => null,
])
@section('vendor-styles')
@once
@select2easyCss()
<link href="{{ asset('vendor/select2easy/select2/css/select2-bootstrap.css') }}" rel='stylesheet'
type='text/css'>
@endonce
@endsection
@php
$id = $id ?? $name;
@endphp
<div class="col-{{ $col }} col-md-{{ $colMd }} {{ $groupClass ?? '' }}">
@if($label)
<x-forms.label
for="{{ $id }}"
class="{{ $labelClass ?? '' }}"
:label="$label"
:isRequired="$attributes->offsetExists('
@props([
'col',
'sl2' => null,
'value' => null,
'name' => null,
'useRequest' => null,
])
@php
$name = $name ?? 'category_id';
$value = isset($useRequest)
? app('request')->input($name)
: $value;
$appModel = '\App\Models\Category';
@endphp
<x-select2.easy
:col="$col ?? '12'"
:col-md="$colMd ?? '4'"
label="Categoria"
:name="$name"
:sl2="$sl2"
:app-model="$appModel"
{{ $attributes }}
>
@if (!empty($value))
<option value="{{ $value }}">
{{ $appModel::find($value)->name }}
</option>
@endif
</x-select2.easy>
# usando request para pegar o value (filtro e etc)
<x-select2.category useRequest data-sl2_child="#subcategory_id"/>
# no form de create/edit
<x-select2.category :value="old('category_id', $model->category_id)" data-sl2_child="#subcategory_id"
composer
php artisan vendor:publish --provider="Gsferro\Select2Easy\Providers\Select2EasyServiceProvider" --force
php
@select2easyCss()
php
@select2easyCss()
php
@select2easyThemeBootstrap5()
php
@select2easyThemeBootstrap5Disabled()
php
@select2easyThemeBootstrap5Advance()
php
@select2easyApplyAnyJs()
php
use Gsferro\Select2Easy\Http\Traits\Select2Easy
class Teams extends Model
{
use Select2Easy;
public static function sl2Name(string $term, int $page, string $parentId = null ) // nome usado na view
{
/*
|---------------------------------------------------
| Required
|---------------------------------------------------
|
| $select2Search - colum from search
| $select2Text - colum from write selectbox
|
*/
$select2Search = [
"name",
// with relation
'relation.title'
];
// Id,
];
$suffix = 'otherRelation.description'; // or other column
return self::select2easy(
$term,
$page,
$select2Search,
$select2Text,
$limitPage = 6,
$extraScopes = [],
$prefix = null,
$scopeParentAndId,
$suffix = null,
);
}
}