I already pass the id from the href
anchor in list page, and still doesn't work. So maybe there is something wrong in controller or route but I'm not seeing it.
Controller
On the controller, I already add the edit and update function, I'm using Resource Controller Laravel-9
public function employee_edit($id)
{
if(Auth::check()){
$empl = employee::findOrFail($id);
return view('edit');
}else{
return redirect('login');
}
}
public function employee_update(Request $request, $id)
{
$input = $request->all();
$employee = employee::find($id);
$employee->update($input);
return redirect('employee');
}
Routes
In web route, I use put method in update route
Route::get('/employee/{$id}/edit', [EmployeeController::class,'employee_edit'])->name('employee.edit');
Route::put('/employee/{$id}/edit', [EmployeeController::class,'employee_update'])->name('employee.update');
Listemployee
This is the list page, as you can see in Edit anchor I already pass the id
@extends('layout.app')
@section('content')
<div class="card">
<div class="card-header">
<a href=""><button class="btn btn-primary position-absolute end-0 ">create new employee</button></a>
<h2>listEmployee</h2>
</div>
<div class="card-body">
<table class="table table-bordered border-dark mt-2 text-center">
<thead class="table table-dark">
<tr>
<th>#</th>
<th>Name</th>
<th>Age</th>
<th>Id employee</th>
</tr>
</thead>
<tbody>
@foreach ($employee as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->age }}</td>
<td>{{ $item->id_employee }}<a href="{{ route('employee.edit',$item->id_employee) }}"><button class="btn btn-primary">Edit</button></a></td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
@endsection
Editemployee
@extends('layout.app')
@section('content')
<div class="card">
<div class="card-header">
<center><h2>EDIT EMPLOYEE</h2></center>
</div>
<div class="card-body">
<form class="form" action="{{ route('employee.edit',$empl->id_employee) }}" method="POST">
@method('put')
{{ csrf_field() }}
<label for="name">Name</label>
<input type="text" name="name" id="name" value="{{ $empl->name }}" class="form-control">
<label for="age">Age</label>
<input type="number" id="age" name="age" value="{{ $empl->age }}" class="form-control">
<center><button class="btn btn-primary mt-2">SUBMIT</button></center>
</form>
</div>
</div>
@endsection
0 comments:
Post a Comment
Thanks