2016-09-15 19:58:27 -07:00
|
|
|
<?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)
|
|
|
|
{
|
2021-06-10 13:15:52 -07:00
|
|
|
return $this->requests->where('canceled_at', null)->where('user_id', $user->id)->first();
|
2016-09-15 19:58:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeRequestedBy($query, User $user)
|
|
|
|
{
|
|
|
|
return $query->whereHas('requests', function ($query) use ($user) {
|
|
|
|
$query->where('user_id', $user->id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
public function request($qty = 1)
|
2016-09-15 19:58:27 -07:00
|
|
|
{
|
|
|
|
$this->requests()->save(
|
2018-04-04 17:33:02 -07:00
|
|
|
new CheckoutRequest(['user_id' => Auth::id(), 'qty' => $qty])
|
2016-09-15 19:58:27 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-04 17:33:02 -07:00
|
|
|
public function deleteRequest()
|
2016-09-15 19:58:27 -07:00
|
|
|
{
|
|
|
|
$this->requests()->where('user_id', Auth::id())->delete();
|
|
|
|
}
|
2018-04-04 17:33:02 -07:00
|
|
|
|
|
|
|
public function cancelRequest()
|
|
|
|
{
|
|
|
|
$this->requests()->where('user_id', Auth::id())->update(['canceled_at' => \Carbon\Carbon::now()]);
|
|
|
|
}
|
2016-09-15 19:58:27 -07:00
|
|
|
}
|