mirror of
https://github.com/brunofontes/shareit.git
synced 2024-11-24 04:14:57 +00:00
32 lines
596 B
PHP
32 lines
596 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();
|
|
}
|
|
}
|