<?php
|
|
|
|
namespace App\Http\Livewire\Collaborators\Associates;
|
|
|
|
use App\Associate;
|
|
use Livewire\Component;
|
|
use Livewire\WithPagination;
|
|
|
|
class Index extends Component
|
|
{
|
|
use WithPagination;
|
|
|
|
/**
|
|
* The parameters that should sync with the URL query string
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $updatesQueryString = [
|
|
'search' => ['except' => ''],
|
|
'associate_category_id' => [],
|
|
'gender' => [],
|
|
];
|
|
|
|
/** @var string */
|
|
public $orderColumn = 'created_at';
|
|
|
|
/** @var string */
|
|
public $orderDirection = 'DESC';
|
|
|
|
/** @var int */
|
|
public $perPage = 10;
|
|
|
|
/** @var string */
|
|
public $search;
|
|
|
|
/** @var int */
|
|
public $associate_category_id;
|
|
|
|
/** @var string */
|
|
public $gender;
|
|
|
|
public function mount()
|
|
{
|
|
$this->search = request()->query('search', $this->search);
|
|
$this->associate_category_id = request()->query('associate_category_id', $this->associate_category_id);
|
|
$this->gender = request()->query('gender', $this->gender);
|
|
}
|
|
|
|
public function updatingSearch()
|
|
{
|
|
$this->resetPage();
|
|
}
|
|
|
|
public function orderBy($orderColumn)
|
|
{
|
|
if ($this->orderColumn === $orderColumn) {
|
|
$this->orderDirection = $this->orderDirection === 'ASC' ? 'DESC' : 'ASC';
|
|
} else {
|
|
$this->orderDirection = 'ASC';
|
|
}
|
|
|
|
$this->orderColumn = $orderColumn;
|
|
|
|
$this->resetPage();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$associates = Associate::with('category')
|
|
->where('name', 'LIKE', '%' . $this->search . '%')
|
|
->orderBy($this->orderColumn, $this->orderDirection)
|
|
->when($this->associate_category_id, function ($query, $associate_category_id) {
|
|
return $query->where('associate_category_id', $associate_category_id);
|
|
})
|
|
->when($this->gender, function ($query, $gender) {
|
|
return $query->where('profile->gender', $gender);
|
|
})
|
|
->paginate($this->perPage);
|
|
|
|
return view('livewire.collaborators.associates.index', compact('associates'));
|
|
}
|
|
}
|