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(
|
2024-09-19 09:31:46 -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
|
|
|
{
|
2024-09-19 09:31:46 -07:00
|
|
|
$this->requests()->where('user_id', auth()->id())->delete();
|
2016-09-15 19:58:27 -07:00
|
|
|
}
|
2018-04-04 17:33:02 -07:00
|
|
|
|
2023-06-27 16:13:11 -07:00
|
|
|
public function cancelRequest($user_id = null)
|
2018-04-04 17:33:02 -07:00
|
|
|
{
|
2023-06-27 16:13:11 -07:00
|
|
|
if (!$user_id){
|
2024-09-19 09:31:46 -07:00
|
|
|
$user_id = auth()->id();
|
2023-06-27 16:13:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->requests()->where('user_id', $user_id)->update(['canceled_at' => \Carbon\Carbon::now()]);
|
2018-04-04 17:33:02 -07:00
|
|
|
}
|
2016-09-15 19:58:27 -07:00
|
|
|
}
|