clara/app/Http/Controllers/JobController.php

140 lines
4.0 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\DataTables\JobDataTable;
use App\Http\Requests\StoreJobRequest;
use App\Http\Requests\UpdateJobRequest;
use App\Models\Directorat;
use App\Models\Job;
use Illuminate\Http\Request;
class JobController extends Controller
{
public $user;
public function __construct()
{
$this->middleware(function ($request, $next) {
//$this->user = Auth::guard('web')->user();
return $next($request);
});
}
/**
* Display a listing of the resource.
*/
public function index(JobDataTable $dataTable, Request $request)
{
/*if (is_null($this->user) || !$this->user->can('masters.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any master data !');
}*/
// Add Vendor
addVendor('chained-select');
if(isset($request->sub_directorat_id) && !empty($request->sub_directorat_id)) {
$this->show($request);
return;
}
$directorat = Directorat::all();
return $dataTable->render('pages.masters.job.index', compact('directorat'));
}
/**
* Show the form for creating a new resource.
*/
public function create(){}
/**
* Store a newly created resource in storage.
*/
public function store(StoreJobRequest $request)
{
/*if (is_null($this->user) || !$this->user->can('masters.create')) {
abort(403, 'Sorry !! You are Unauthorized to create any master data !');
}*/
// Validate the request...
$validated = $request->validated();
// Store the Job...
if($validated){
try{
Job::create($validated);
//return redirect()->route('job.index')->with('success', 'Job created successfully.');
echo json_encode(['status' => 'success', 'message' => 'Job created successfully.']);
}catch(\Exception $e){
//return redirect()->route('job.index')->with('error', 'Job created failed.');
echo json_encode(['status' => 'error', 'message' => 'Job created failed.']);
}
}
return false;
}
/**
* Display the specified resource.
*/
public function show(Request $request)
{
$jobs = Job::where('sub_directorat_id', $request->sub_directorat_id)->get();
$data = [];
foreach ($jobs as $row) {
$result = [
$row->id => $row->name,
];
$data[] = $result;
}
echo json_encode($data);
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id){
$job = Job::find($id);
echo json_encode($job);
}
/**
* Update the specified resource in storage.
*/
public function update(UpdateJobRequest $request, Job $job)
{
/*if (is_null($this->user) || !$this->user->can('masters.update')) {
abort(403, 'Sorry !! You are Unauthorized to update any master data !');
}*/
// Validate the request...
$validated = $request->validated();
// Update the Job...
if($validated){
try{
$job->update($validated);
//return redirect()->route('job.index')->with('success', 'Job updated successfully.');
echo json_encode(['status' => 'success', 'message' => 'Job updated successfully.']);
}catch(\Exception $e){
//return redirect()->route('job.index')->with('error', 'Job updated failed.');
echo json_encode(['status' => 'error', 'message' => 'Job updated failed.']);
}
}
return false;
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Job $job){
$job->delete();
echo json_encode(['status' => 'success', 'message' => 'Job deleted successfully.']);
//return redirect()->route('job.index')->with('success', 'Job deleted successfully.');
}
}