Core/app/Http/Controllers/DirectoratController.php

124 lines
3.7 KiB
PHP
Raw Normal View History

2023-04-11 09:21:20 +00:00
<?php
namespace App\Http\Controllers;
use App\DataTables\DirectoratDataTable;
use App\Http\Requests\StoreDirectoratRequest;
use App\Http\Requests\UpdateDirectoratRequest;
use App\Models\Directorat;
use Illuminate\Http\Request;
use Spatie\Activitylog\Facades\CauserResolver;
2023-04-11 09:21:20 +00:00
class DirectoratController extends Controller
{
public $user;
public function __construct()
{
$this->middleware(function ($request, $next) {
2023-04-12 01:30:25 +00:00
//$this->user = Auth::guard('web')->user();
2023-04-11 09:21:20 +00:00
return $next($request);
});
//CauserResolver::setCauser($this->user);
2023-04-11 09:21:20 +00:00
}
/**
* Display a listing of the resource.
*/
public function index(DirectoratDataTable $dataTable)
{
/*if (is_null($this->user) || !$this->user->can('masters.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any master data !');
}*/
return $dataTable->render('pages.masters.directorat.index');
}
/**
* Show the form for creating a new resource.
*/
2023-04-12 10:06:33 +00:00
public function create(){}
2023-04-11 09:21:20 +00:00
/**
* Store a newly created resource in storage.
*/
public function store(StoreDirectoratRequest $request)
{
/*if (is_null($this->user) || !$this->user->can('masters.create')) {
abort(403, 'Sorry !! You are Unauthorized to create any master data !');
}*/
2023-04-12 10:06:33 +00:00
2023-04-11 09:21:20 +00:00
// Validate the request...
$validated = $request->validated();
// Store the Directorat...
if($validated){
try{
Directorat::create($validated);
2023-04-12 10:06:33 +00:00
//return redirect()->route('directorat.index')->with('success', 'Directorat created successfully.');
echo json_encode(['status' => 'success', 'message' => 'Directorat created successfully.']);
2023-04-11 09:21:20 +00:00
}catch(\Exception $e){
2023-04-12 10:06:33 +00:00
//return redirect()->route('directorat.index')->with('error', 'Directorat created failed.');
echo json_encode(['status' => 'error', 'message' => 'Directorat created failed.']);
2023-04-11 09:21:20 +00:00
}
}
return false;
}
/**
* Display the specified resource.
*/
public function show(Directorat $directorat)
{
2023-04-11 09:21:20 +00:00
}
/**
* Show the form for editing the specified resource.
*/
2023-04-12 10:06:33 +00:00
public function edit($id){
2023-04-11 09:21:20 +00:00
$directorat = Directorat::find($id);
2023-04-12 10:06:33 +00:00
echo json_encode($directorat);
2023-04-11 09:21:20 +00:00
}
/**
* Update the specified resource in storage.
*/
public function update(UpdateDirectoratRequest $request, Directorat $directorat)
{
/*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 Directorat...
if($validated){
try{
CauserResolver::setCauser($this->user);
2023-04-11 09:21:20 +00:00
$directorat->update($validated);
2023-04-12 10:06:33 +00:00
//return redirect()->route('directorat.index')->with('success', 'Directorat updated successfully.');
echo json_encode(['status' => 'success', 'message' => 'Directorat updated successfully.']);
2023-04-11 09:21:20 +00:00
}catch(\Exception $e){
2023-04-12 10:06:33 +00:00
//return redirect()->route('directorat.index')->with('error', 'Directorat updated failed.');
echo json_encode(['status' => 'error', 'message' => 'Directorat updated failed.']);
2023-04-11 09:21:20 +00:00
}
}
return false;
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Directorat $directorat){
$directorat->delete();
echo json_encode(['status' => 'success', 'message' => 'Directorat deleted successfully.']);
2023-04-11 09:21:20 +00:00
}
}