Hi I am having trouble displaying vacancies on the page
<?php
namespace App\Http\Controllers;
use App\Models;
use App\Models\Category;
use App\Services\VacancyService;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use App\Models\Vacancy;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rules\File;
use App\Actions\AddVacancyAction;
use App\Actions\UpdateVacancyAction;
use App\Actions\DestroyVacancyAction;
use Illuminate\Database\Eloquent\Model;
use App\Http\Requests\StoreVacancyRequest;
class VacancyController extends Controller
{
protected $service;
// Other model methods and properties
//tbc add CRUD action functions- see week 4 notes
//start by implmenting index action- see week 4 index action and view
public function index(Request $request)
{
if (!Gate::allows('viewAny', Vacancy::class)) {
return redirect()->route('login')->with('info', 'Please Login to view vacancies');
}
$size = $request->input('size', 10);
$sort = $request->input('sort', 'id');
$direction = $request->input('direction', 'asc');
$search = $request->query('search', null);
// $vacancies = Vacancy::with(['category'])
// ->search($search)
// ->sortable($sort, $direction)
// ->paginate($size)
// ->withQueryString();
$vacancies = $this->service->findAll($search, $sort, $direction, $size);
return view('vacancies.index', ['vacancies' => $vacancies, 'search' => $search]);
} _
// display list of vacancies _ public function create() {
if (!Gate::allows('create', Vacancy::class)) {
return redirect()->back()
->with('warning', 'Not authorised');
} else {
$vacancy = new Vacancy;
$categories = $this->service->getCategorySelectList();
return view('vacancies.create', ['vacancy' => $vacancy, 'categories' => $categories]);
} // show create vacancy form
}
public function store(Request $request, Vacancy $service)
{
Gate::allows('create', Vacancy::class);
$data = $request->validate([
'title' => ['required'],
'closing date' => ['required'],
'category_id' => ['required'],
'skills required' => ['required', 'numeric'],
'location' => ['required', 'numeric', 'min:0', 'max:5'],
'job description' => ['min:0', 'max:500'],
['category_id' => 'The category field is required']
]);
// store new vacancy
$vacancy = $service->create($data);
if(!$vacancy){
return redirect()->route('vacancies.index')->with('error', 'Failed to create vacancy');
}
return redirect()->route('vacancies.show', $vacancy->id)->with('success', 'Vacancy created successfully');
}
// public function store(StoreVacancyRequest $request, AddVacancyAction $action)
public function show(int $id)
{
if (!Gate::allows('viewAny', Vacancy::class)) {
return redirect()->route('login')->with('info', 'Please Login to view vacancies');
}
$vacancy = Vacancy::findOrFail($id);
return view('vacancies.show', ['vacancy' => $vacancy]);
} // display specified vacancy
public function edit(int $id)
{
if (!Gate::allows('update', Vacancy::class)) {
return redirect()->route('login')->with('info', 'Please Login to edit a vacancy');
}
$vacancy = Vacancy::findOrFail($id);
$categories = Category::all()->pluck('name', 'id');
return view('vacancies.edit', ['vacancy' => $vacancy, 'categories' => $categories]);
}
public function update(int $id, array $data, Request $request)
{
if (!Gate::allows('update', Vacancy::class)) {
return redirect()->back()
->with('warning', 'Not authorised');
}
$data = $request->validate([
'title' => ['required', Rule::unique('vacancies')->ignore($id)],
'category_id' => ['required'],
'closing_date' => ['required'],
'skills_required' => ['required', 'numeric'],
'location' => ['required', 'numeric', 'min:0', 'max:5'],
'job_description' => ['min:0', 'max:500'],
'image' => ['nullable', File::types(['png', 'jpg', 'jpeg', 'webp'])->max(1024)],
]);
$vacancy = $this->service->update($id, $data);
if ($vacancy === null) {
return redirect()->route('vacancies.index')
->with('error', 'Vacancy not found');
}
return redirect()->route("vacancies.show", $id)
->with('success', 'Vacancy updated successfully');
}
// Other methods like destroy, create, etc.
public function delete(int $id)
{
// authorise the deletion
Gate::authorize('delete', Vacancy::class);
if ($this->service->delete($id)) {
return redirect()->route('vacancies.index')->with('success', 'Vacancy deleted successfully');
} else {
return redirect()->route('vacancies.index')->with('error', 'Vacancy not found');
}
}
}
// show edit vacancy form
// public function update(Request $request, string $id) { … ;} // update
//public function destroy(int $id) { … ;} // remove specified vacancy
Author Of article : Thomas Costigan Read full article