<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
thiktak / laravel-bootstrap-component-select2 example snippets
use Thiktak\LaravelBootstrapComponentSelect2\Models\Search2Proxies\Select2Searchable;
class User extends Model
{
use Select2Searchable;
// Will use magic search
protected $search2Fields = ['name', 'title'];
// [...]
}
namespace App\Models\Search2Proxies;
use Thiktak\LaravelBootstrapComponentSelect2\Models\Search2Proxies\Select2Proxy;
class UserProxy extends Select2Proxy
{
/*
* protected $search2Fields = ['name', 'title'];
*/
/**
* METHOD getModel
*----------------
* Define your own model
* Used by magic method select2, if not overwritten
*/
public function getModel() {
return new \App\Models\User;
}
/**
* Method Select2
*----------------
* Will return a query object based on the keyword searched
* ... or the ID provided
*/
public function select2($id, $term) {
return $this->getModel()
->query()
// Search by ID
->when($id, function($q) use($id) {
// no return
$q->find($id);
// OR $q->where($this->getModel()->getPrimaryKey(), $id)
})
->when(!$id, function($q) use($term) {
return $q
->where('name', 'like', '%' . $term . '%')
->orderBy('name');
});
}
/**
* Method Export_select2
* Will export array data based on Select2 format id/text
*/
public function export_select2(User $user) {
// id => '1'
// text => 'Admin ([email protected])'
return [
'id' => $user->id,
'text' => sprintf('%s (%s)', $user->name, $user->email),
];
}
// [...]
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.