update from request

This commit is contained in:
Daeng Deni Mardaeni 2023-10-09 18:12:51 +07:00
parent c1543263c4
commit cf61d5d53f
4 changed files with 40 additions and 74 deletions

View File

@ -42,31 +42,12 @@
/**
* Store a newly created Branch in storage.
*
* @param \Modules\Writeoff\Http\Requests\Branch\StoreBranchRequest $request
* @param \Illuminate\Http\Request $request
*
* @return mixed
*/
public function store(StoreBranchRequest $request)
public function store(Request $request)
{
if (is_null($this->user) || !$this->user->can('master.create')) {
abort(403, 'Sorry !! You are Unauthorized to create any master data !');
}
// Validate the request...
$validated = $request->validated();
// Store the Branch...
if ($validated) {
try {
Branch::create($validated);
echo json_encode(['status' => 'success', 'message' => 'Branch created successfully.']);
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => 'Branch created failed.']);
}
return;
}
echo json_encode(['status' => 'error', 'message' => 'Branch created failed.']);
}
/**
@ -74,23 +55,17 @@
*/
public function create()
{
if (is_null($this->user) || !$this->user->can('master.create')) {
abort(403, 'Sorry !! You are Unauthorized to create any master data !');
}
abort(404);
}
/**
* Display the specified Branch.
*
* @param \Modules\Writeoff\Entities\Branch $directorat
* @param \Modules\Writeoff\Entities\Branch $branch
*/
public function show(Branch $directorat)
public function show(Branch $branch)
{
if (is_null($this->user) || !$this->user->can('master.read')) {
abort(403, 'Sorry !! You are Unauthorized to view any master data !');
}
}
/**
@ -100,61 +75,31 @@
*/
public function edit($id)
{
if (is_null($this->user) || !$this->user->can('master.update')) {
abort(403, 'Sorry !! You are Unauthorized to update any master data !');
}
$directorat = Branch::find($id);
echo json_encode($directorat);
}
/**
* Update the specified Branch in storage.
*
* @param \Modules\Writeoff\Http\Requests\Branch\UpdateBranchRequest $request
* @param \Modules\Writeoff\Entities\Branch $directorat
* @param \Illuminate\Http\Request $request
* @param \Modules\Writeoff\Entities\Branch $branch
*
* @return mixed
*/
public function update(UpdateBranchRequest $request, Branch $directorat)
public function update(Request $request, Branch $branch)
{
if (is_null($this->user) || !$this->user->can('master.update')) {
abort(403, 'Sorry !! You are Unauthorized to update any master data !');
}
// Validate the request...
$validated = $request->validated();
// Update the Branch...
if ($validated) {
try {
$directorat->update($validated);
echo json_encode(['status' => 'success', 'message' => 'Branch updated successfully.']);
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => 'Branch updated failed.']);
}
return;
}
echo json_encode(['status' => 'error', 'message' => 'Branch updated failed.']);
}
/**
* Remove the specified Branch from storage.
*
* @param \Modules\Writeoff\Entities\Branch $directorat
* @param \Modules\Writeoff\Entities\Branch $branch
*
* @return void
*/
public function destroy(Branch $directorat)
public function destroy(Branch $branch)
{
if (is_null($this->user) || !$this->user->can('master.delete')) {
abort(403, 'Sorry !! You are Unauthorized to delete any master data !');
}
$directorat->delete();
echo json_encode(['status' => 'success', 'message' => 'Branch deleted successfully.']);
}
}

View File

@ -28,11 +28,16 @@
: array
{
return [
'kode' => 'required|string|max:9|min:9|unique:branches,kode',
'kode' => 'required|string|max:9|min:9|unique:branches,kode',$this->branches->id,
'name' => 'required|string|max:100'
];
}
public function ignored(): string
{
return $this->id;
}
/**
* Configure the validator instance.
*/

View File

@ -27,8 +27,8 @@
public function rules()
: array
{
return [
'kode' => 'required|string|max:9|min:9|unique:branches,kode,' . $this->branch->id,
return [t
'kode' => 'required|string|max:9|min:9|unique:branches,kode,'.$this->request->id,
'name' => 'required|string|max:100'
];
}
@ -62,4 +62,10 @@
'messages' => 'Branch updated failed.'
], JsonResponse::HTTP_UNPROCESSABLE_ENTITY));
}
protected function prepareForValidation()
{
print_r($this->all());
}
}

View File

@ -5,19 +5,17 @@
use Illuminate\Support\Facades\DB;
use Livewire\Component;
use Modules\Writeoff\Entities\Branch;
use Modules\Writeoff\Http\Requests\Branch\StoreBranchRequest;
use Modules\Writeoff\Http\Requests\Branch\UpdateBranchRequest;
class BranchModal extends Component
{
public $id;
public $kode;
public $name;
public $edit_mode = false;
protected $rules = [
'kode' => 'required|string|max:255|unique:branches,kode',
'name' => 'required|string',
];
protected $listeners = [
'delete_branch' => 'deleteBranch',
'update_branch' => 'updateBranch',
@ -28,11 +26,22 @@
return view('writeoff::livewire.branch.branch-modal');
}
protected function rules()
{
if ($this->edit_mode) {
$request = new UpdateBranchRequest();
} else {
$request = new StoreBranchRequest();
}
return $request->rules();
}
public function submit()
{
// Validate the form input data
$this->validate();
// Validate the form input data
DB::transaction(function () {
// Prepare the data for creating a new user
$data = [
@ -71,6 +80,7 @@
$branch = Branch::find($id);
$this->id = $branch->id;
$this->kode = $branch->kode;
$this->name = $branch->name;
}