Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

nafiesl/free-pmo

457 stars
2 code files
View nafiesl/free-pmo on GitHub

app/Entities/Projects/ProjectsRepository.php

Open in GitHub
use App\Entities\BaseRepository;
use App\Entities\Users\User;
use ProjectStatus;
 
class ProjectsRepository extends BaseRepository
{
protected $model;
 
public function __construct(Project $model)
{
parent::__construct($model);
}
 
public function getProjects($q, $statusId, User $user)
{
$statusIds = array_keys(ProjectStatus::toArray());
 
if ($user->hasRole('admin') == false) {
return $user->projects()
->where(function ($query) use ($q, $statusId, $statusIds) {
$query->where('projects.name', 'like', '%'.$q.'%');
 
if ($statusId && in_array($statusId, $statusIds)) {
$query->where('status_id', $statusId);
}
})
->latest()
->with(['customer', 'jobs'])
->paginate($this->_paginate);
}
 
return $this->model->latest()
->where(function ($query) use ($q, $statusId, $statusIds) {
$query->where('name', 'like', '%'.$q.'%');
 
if ($statusId && in_array($statusId, $statusIds)) {
$query->where('status_id', $statusId);
}
})
->with('customer')
->paginate($this->_paginate);
}
//
public function getStatusName($statusId)
{
return ProjectStatus::getNameById($statusId);
}
//
}

app/Http/Controllers/Projects/ProjectsController.php

Open in GitHub
use App\Entities\Projects\ProjectsRepository;
use Illuminate\Http\Request;
 
class ProjectsController extends Controller
{
private $repo;
 
public function __construct(ProjectsRepository $repo)
{
$this->repo = $repo;
}
public function index(Request $request)
{
$status = null;
$statusId = $request->get('status_id');
if ($statusId) {
$status = $this->repo->getStatusName($statusId);
}
 
$projects = $this->repo->getProjects($request->get('q'), $statusId, auth()->user());
 
return view('projects.index', compact('projects', 'status', 'statusId'));
}
//
}