Thanks all for replying. The below is the content of GetEmployeesTableRequest.php
<?php
namespace App\Http\Requests\Dashboard\Employees;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use App\Exceptions\Authorization\GetEmployeeListPermissionException;
use Illuminate\Validation\ValidationException;
class GetEmployeesTableRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return Gate::allows('get_employees_list');
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
//
}
public function messages()
{
//
}
protected function failedAuthorization()
{
throw new GetEmployeeListPermissionException();
}
protected function failedValidation($validator)
{
throw new ValidationException($validator, jsonResponse($validator->errors()->first()));
}
}
And this is content of EmployeeController.php
<?php
namespace App\Http\Controllers\Dashboard;
use App\Http\Controllers\Controller;
use App\User;
use App\Role;
use App\Http\Requests\Dashboard\Employees\GetEmployeesTableRequest;
class EmployeeController extends Controller
{
/**
* Display a listing of the employees.
*
* @return \Illuminate\Http\Response
*/
public function index(GetEmployeesTableRequest $request)
{
// get Employee list
// Setup pagination data
$perPage = $request->query('per_page') ?? 20;
$currentPage = $request->query('page') ?? 1;
// Setup Sorting
$sortBy = $request->query('sort_by') ?? 'id';
$order = $request->query('order') ?? 'desc';
$roles = $request->query('roles') ?? '';
$status = $request->query('resigned') ?? '';
// Setup main query
$data = User::with(['roles' => function ($q) use ($sortBy, $order) {
$q->select('id', 'name');
if ($sortBy === 'roles') {
$q->orderBy('roles.name', $order);
}
}])->where('type', 'employee');
// Setup roles filter
if ($request->query('roles')) {
$data = $data->whereHas('roles', function ($q) use ($request) {
$q->where('id', $request->query('roles'));
});
}
// Setup status filter
if (is_numeric($request->query('resigned'))
&& ($request->query('resigned') == 0 || $request->query('resigned') == 1 ))
{
$data->where('resigned', $request->query('resigned'));
}
// Setup keyword
if ($request->query('keyword')) {
$keyword = explode(" ", trim($request->query('keyword')));
foreach ($keyword as $k) {
$data = $data->where(function($q) use ($k) {
$q->orWhere('name', 'LIKE', "%$k%");
$q->orWhere('email', 'LIKE', "%$k%");
$q->orWhere('tel', 'LIKE', "%$k%");
$q->orWhere('address', 'LIKE', "%$k%");
$q->orWhere('facebook', 'LIKE', "%$k%");
});
}
}
if ($sortBy !== 'roles') {
$data = $data->orderBy($sortBy, $order);
}
$data = $data->paginate($perPage, ['*'], 'page', $currentPage);
return view('dashboard.employees.index', [
'employees' => $data,
'roles' => Role::all('id', 'name'),
]);
}
}
To @Snapey, I also though that it is the case sensitive error, but after checked, I can sure that is is impossible, because:
- I created these classes via Laravel commnd, and using Eclipse IDE to develop, which help me a lot for auto completing the method name, propertiy names, as well as import declaration etc.
- The app worked fine too when I run it to my Virtual machine which using Ubuntu 18.04 LTS (my VPS run Centos 7)