Added companies ajax select2 endpoint

This commit is contained in:
snipe
2017-10-26 16:37:41 -07:00
parent 1fa6228fb7
commit bdb95e4e3d
3 changed files with 71 additions and 0 deletions
@@ -141,4 +141,57 @@ class CompaniesController extends Controller
}
}
/**
* Display a formatted JSON response for the select2 menus
*
* @todo: create a transformer for handling these responses
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0]
*
* @return \Illuminate\Http\Response
*/
public function selectlist(Request $request)
{
$this->authorize('view', Company::class);
$companies = Company::select([
'companies.id',
'companies.name',
'companies.image',
]);
if ($request->has('search')) {
$companies = $companies->where('companies.name', 'LIKE', '%'.$request->get('search').'%');
}
$companies = $companies->paginate(50);
$companies_array = [];
foreach ($companies as $company) {
$companies_array[] =
[
'id' => (int) $company->id,
'text' => e($company->name),
'image' => ($company->image) ? url('/').'/uploads/companies/'.$company->image : null,
];
}
array_unshift($companies_array, ['id'=> '', 'text'=> trans('general.select_company'), 'image' => null]);
$results = [
'items' => $companies_array,
'pagination' =>
[
'more' => ($companies->currentPage() >= $companies->lastPage()) ? false : true,
'per_page' => $companies->perPage()
],
'total_count' => $companies->total(),
'page' => $companies->currentPage(),
'page_count' => $companies->lastPage()
];
return $results;
}
}