mirror of
https://github.com/snipe/snipe-it.git
synced 2024-11-14 01:24:06 -08:00
e3e0d57f56
* Add IDE Helper files * Cleanup imports - Alphabetises imports - Removes unused imports * Add Platform requirements * Move filling asset into block where asset exists * Remove duplicate array keys
47 lines
1.1 KiB
PHP
47 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
// $asset->requests
|
|
// $asset->isRequestedBy($user)
|
|
// $asset->whereRequestedBy($user)
|
|
trait Requestable
|
|
{
|
|
|
|
public function requests()
|
|
{
|
|
return $this->morphMany(CheckoutRequest::class, 'requestable');
|
|
}
|
|
|
|
public function isRequestedBy(User $user)
|
|
{
|
|
return $this->requests->where('canceled_at', NULL)->where('user_id', $user->id)->first();
|
|
}
|
|
|
|
public function scopeRequestedBy($query, User $user)
|
|
{
|
|
return $query->whereHas('requests', function ($query) use ($user) {
|
|
$query->where('user_id', $user->id);
|
|
});
|
|
}
|
|
|
|
public function request($qty = 1)
|
|
{
|
|
$this->requests()->save(
|
|
new CheckoutRequest(['user_id' => Auth::id(), 'qty' => $qty])
|
|
);
|
|
}
|
|
|
|
public function deleteRequest()
|
|
{
|
|
$this->requests()->where('user_id', Auth::id())->delete();
|
|
}
|
|
|
|
public function cancelRequest()
|
|
{
|
|
$this->requests()->where('user_id', Auth::id())->update(['canceled_at' => \Carbon\Carbon::now()]);
|
|
}
|
|
}
|