dimanche 28 janvier 2018

Laravel: how to use softDelete on common polymorph model

I have a table of Items with softDelete enabled:

class Item extends Model
{
  use SoftDeletes;

  protected $hidden = [
      'extendable_type', 'extendable_id'
  ];

  public function extendable()
  {
      return $this->morphTo();
  }

and several classes that share a relationship with the Items via traits:

trait IsItemModel
{

  public function item()
  {
      return $this->morphOne('App\Models\Item', 'extendable')->withDefault();
  }

and

class FileItem implements ItemInterface
{
  use IsItem;
  ...
  public static function delete(Request $request)
  {
    $file = self::$model::deletable()->find($request->itemId);
    $file->delete();
    return true;
  }

However, the call to $file->delete() always just hard-deletes the row in the file_items table, instead of setting deleted_at in the common items table.

I could just apply softDelete to each individual model, but that seems wrong somehow. Is there any way to do this so the softDelete functionality is shared by the polymorphic relationship?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire