Skip to content

Commit

Permalink
feat: History Peminjaman
Browse files Browse the repository at this point in the history
  • Loading branch information
malikfajr committed Jul 22, 2023
1 parent 759c444 commit 3acb516
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 3 deletions.
16 changes: 15 additions & 1 deletion app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Models\Barang;
use App\Models\History;
use App\Models\Pinjam;
use Illuminate\Http\Request;

Expand All @@ -11,7 +12,7 @@ class DashboardController extends Controller
/**
* Handle the incoming request.
*/
public function __invoke(Request $request)
public function index(Request $request)
{
$pinjam = Pinjam::query();

Expand All @@ -27,4 +28,17 @@ public function __invoke(Request $request)
'pinjam' => $pinjam->get(),
]);
}

public function history() {
$histories = History::query();

if (! auth()->user()->is_admin) {
$histories->where('user_id', auth()->user()->id);
}
$histories->orderBy('created_at', 'desc');

return view('history', [
'histories' => $histories->get()
]);
}
}
15 changes: 15 additions & 0 deletions app/Models/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class History extends Model
{
Expand All @@ -18,4 +20,17 @@ class History extends Model
'date',
'status',
];

public function user() : BelongsTo {
return $this->belongsTo(User::class, 'user_id', 'id');
}

public function barang(): BelongsTo
{
return $this->belongsTo(Barang::class, 'barang_id', 'id');
}

public function getDateAttribute ($value) {
return (new Carbon($value))->locale('id')->translatedFormat('l, d F Y');
}
}
9 changes: 9 additions & 0 deletions app/Models/Pinjam.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand All @@ -28,4 +29,12 @@ public function barang() {
public function user() {
return $this->belongsTo(User::class);
}

public function getStartingDateAttribute ($value) {
return (new Carbon($value))->locale('id')->translatedFormat('l, d F Y');
}

public function getEndingDateAttribute ($value) {
return (new Carbon($value))->locale('id')->translatedFormat('l, d F Y');
}
}
78 changes: 78 additions & 0 deletions resources/views/history.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Dashboard') }}
</h2>
</x-slot>

<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="flex flex-col">
<div class="my-2 overflow-x-auto sm:-mx-6 lg:-mx-8">
<div class="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8">
@if (! auth()->user()->is_admin)
<a href="{{ route('list.barang') }}" class="px-6">
<x-primary-button>Daftar Barang</x-primary-button>
</a>
@endif
</div>
</div>
<div class="overflow-x-auto sm:-mx-6 lg:-mx-8">
<div class="inline-block min-w-full py-2 sm:px-6 lg:px-8">
<div class="overflow-hidden">
<table class="min-w-full text-left text-sm font-light">
<thead class="border-b bg-white font-medium ">
<tr>
<th scope="col" class="px-6 py-4">Nama Peminjam</th>
<th scope="col" class="px-6 py-4">Nama Admin</th>
<th scope="col" class="px-6 py-4">Nama Barang</th>
<th scope="col" class="px-6 py-4">Qty</th>
<th scope="col" class="px-6 py-4">Tanggal</th>
<th scope="col" class="px-6 py-4">Status</th>
</tr>
</thead>
<tbody>

@forelse ($histories as $history)
<tr class="border-b bg-white even:bg-neutral-100">
<td class="whitespace-nowrap px-6 py-4">{{ $history->user->name }}</td>
<td class="whitespace-nowrap px-6 py-4">{{ $history->admin_name ?? '' }}</td>
<td class="whitespace-nowrap px-6 py-4">{{ $history->barang->nama }}</td>
<td class="whitespace-nowrap px-6 py-4">{{ $history->qty }}</td>
<td class="whitespace-nowrap px-6 py-4">{{ $history->date }}</td>
<td class="whitespace-nowrap px-6 py-4">
@switch($history->status)
@case('diajukan')
<span class="px-3 py-2 text-sm rounded-md font-bold bg-yellow-300">Diajukan</span>
@break
@case('dipinjam')
<span class="px-3 py-2 text-sm rounded-md font-bold bg-green-600">Dipinjam</span>
@break
@case('dikembalikan')
<span class="px-3 py-2 text-sm rounded-md font-bold bg-blue-600">Dikembalikan</span>
@break
@case('dibatalkan')
<span class="px-3 py-2 text-sm rounded-md font-bold bg-red-600">Dibatalkan</span>
@break
@default
<span class="px-3 py-2 text-sm rounded-md font-bold bg-red-600">Ditolak</span>
@endswitch
</td>
</tr>
@empty
<tr class="border-b bg-white even:bg-neutral-100">
<td class="whitespace-nowrap px-6 py-4 text-center font-bold" colspan="7">Data Belum Ada</td>
</tr>
@endforelse

</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</x-app-layout>
2 changes: 1 addition & 1 deletion resources/views/layouts/navigation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</x-nav-link>
@endif

<x-nav-link :href="route('dashboard')" :active="request()->routeIs('dashboard')">
<x-nav-link :href="route('history')" :active="request()->routeIs('history')">
{{ __('History') }}
</x-nav-link>
</div>
Expand Down
3 changes: 2 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
return view('welcome');
});

Route::get('/dashboard', DashboardController::class)->middleware(['auth', 'verified'])->name('dashboard');
Route::get('/dashboard', [DashboardController::class, 'index'])->middleware(['auth', 'verified'])->name('dashboard');
Route::get('/history', [DashboardController::class, 'history'])->middleware(['auth', 'verified'])->name('history');

Route::middleware('auth')->group(function () {
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Expand Down

0 comments on commit 3acb516

Please sign in to comment.