TKK_E32230908/app/Models/Book.php

41 lines
791 B
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
use HasFactory;
protected $fillable = [
'judul',
'penulis',
'penerbit',
'tahun_terbit',
'kategori',
'jumlah_eksemplar',
];
public function borrowTransactions()
{
return $this->hasMany(BorrowTransaction::class, 'book_id');
}
public function availableCopies()
{
$borrowed = $this->borrowTransactions()
->where('status', 'dipinjam')
->count();
return $this->jumlah_eksemplar - $borrowed;
}
public function getFormattedTitleAttribute()
{
return ucwords(strtolower($this->judul));
}
}