1+ <?php
2+
3+ namespace App \Http \Controllers ;
4+
5+ use App \Models \Job ;
6+ use Illuminate \Http \Request ;
7+
8+ class MyJobController extends Controller
9+ {
10+ public function __construct ()
11+ {
12+ $ this ->authorizeResource (Job::class, 'my_job ' );
13+ }
14+
15+ public function index ()
16+ {
17+ return view (
18+ 'my_job.index ' ,
19+ [
20+ 'jobs ' => auth ()->user ()->employer ->jobs ()->with ('employer ' )->get ()
21+ ]
22+ );
23+ }
24+
25+ public function create ()
26+ {
27+ return view ('my_job.create ' );
28+ }
29+
30+ public function store (Request $ request )
31+ {
32+ $ validatedData = $ request ->validate ([
33+ 'title ' => 'required|string|max:255 ' ,
34+ 'location ' => 'required|string|max:255 ' ,
35+ 'salary ' => 'required|numeric|min:5000 ' ,
36+ 'description ' => 'required|string ' ,
37+ 'experience ' => 'required|in: ' . implode (', ' , Job::$ experience ),
38+ 'category ' => 'required|in: ' . implode (', ' , Job::$ category ),
39+ ]);
40+
41+ $ job = $ request ->user ()->employer ->jobs ()->create ($ validatedData );
42+
43+ return redirect ()->route ('my-jobs.index ' )
44+ ->with ('success ' , 'Job created successfully. ' );
45+ }
46+
47+ public function edit (Job $ myJob )
48+ {
49+ return view ('my_job.edit ' , ['job ' => $ myJob ]);
50+ }
51+
52+ public function update (Request $ request , Job $ myJob )
53+ {
54+ $ validatedData = $ request ->validate ([
55+ 'title ' => 'required|string|max:255 ' ,
56+ 'location ' => 'required|string|max:255 ' ,
57+ 'salary ' => 'required|numeric|min:5000 ' ,
58+ 'description ' => 'required|string ' ,
59+ 'experience ' => 'required|in: ' . implode (', ' , Job::$ experience ),
60+ 'category ' => 'required|in: ' . implode (', ' , Job::$ category ),
61+ ]);
62+
63+ $ myJob ->update ($ validatedData );
64+
65+ return redirect ()->route ('my-jobs.index ' )
66+ ->with ('success ' , 'Job updated successfully. ' );
67+ }
68+
69+ public function destroy (Job $ myJob )
70+ {
71+ $ myJob ->delete ();
72+
73+ return redirect ()->route ('my-jobs.index ' )
74+ ->with ('success ' , 'Job deleted successfully. ' );
75+ }
76+ }
0 commit comments