mirror of
https://github.com/brunofontes/shareit.git
synced 2024-11-27 21:31:27 +00:00
Bruno Fontes
528d3b4caf
The TakeController was manually sending the email message as well as dealing with item return, so I moved the email message to it's own listener, I created an event for it and moved the return part to the item model.
54 lines
1018 B
PHP
54 lines
1018 B
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class Item extends Model
|
|
{
|
|
protected $fillable = ['product_id', 'name'];
|
|
|
|
public function product()
|
|
{
|
|
return $this->belongsTo(Product::class);
|
|
}
|
|
|
|
public function users()
|
|
{
|
|
return $this->belongsToMany(User::class);
|
|
}
|
|
|
|
public static function deleteAndDetach($item)
|
|
{
|
|
//Detach users from this item
|
|
foreach ($item->users as $user) {
|
|
User::findOrFail($user->id)->items()->detach($item->id);
|
|
}
|
|
|
|
//Delete item
|
|
$item->delete();
|
|
}
|
|
|
|
/**
|
|
* Return the items from logged in user
|
|
*
|
|
* @return \App\Item
|
|
*/
|
|
public static function fromAuthUser()
|
|
{
|
|
return (new static)->where('user_id', \Auth::id());
|
|
}
|
|
|
|
/**
|
|
* Return a specified item
|
|
*
|
|
* @return void
|
|
*/
|
|
public function returnItem()
|
|
{
|
|
$this->used_by = null;
|
|
$this->waiting_user_id = null;
|
|
$this->save();
|
|
}
|
|
}
|