2018-09-09 03:26:57 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
|
|
class Item extends Model
|
|
|
|
{
|
2018-09-11 23:48:09 +00:00
|
|
|
protected $fillable = ['product_id', 'name'];
|
|
|
|
|
|
|
|
public function product()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Product::class);
|
|
|
|
}
|
|
|
|
|
2018-09-12 05:31:03 +00:00
|
|
|
public function users()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(User::class);
|
|
|
|
}
|
|
|
|
|
2018-09-17 03:51:41 +00:00
|
|
|
public static function deleteAndDetach($item)
|
2018-09-09 03:26:57 +00:00
|
|
|
{
|
2018-09-17 03:51:41 +00:00
|
|
|
//Detach users from this item
|
|
|
|
foreach ($item->users as $user) {
|
|
|
|
User::findOrFail($user->id)->items()->detach($item->id);
|
|
|
|
}
|
2018-09-09 03:26:57 +00:00
|
|
|
|
2018-09-17 03:51:41 +00:00
|
|
|
//Delete item
|
|
|
|
$item->delete();
|
|
|
|
}
|
2018-09-19 17:58:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the items from logged in user
|
|
|
|
*
|
|
|
|
* @return \App\Item
|
|
|
|
*/
|
|
|
|
public static function fromAuthUser()
|
|
|
|
{
|
|
|
|
return (new static)->where('user_id', \Auth::id());
|
|
|
|
}
|
2018-09-09 03:26:57 +00:00
|
|
|
}
|