src/Eloquent/Repository.php
public function deleteMorphsByEntity(IlluminateEloquentModel $entity, $relationName, $forceDelete = false)
    {
        // Retrieve all records by entity type and entity ID
        $entities = $this->getBuilder()
                         ->select(['id'])
                         ->where(function ($query) use ($entity, $relationName) {
                             $query->where($relationName.'_type', '=', get_class($entity))
                                   ->where($relationName.'_id', '=', (int) $entity->id);
                         })
                         ->get();

        // Delete count
        $deleteCount = 0;

        // Loop through each entity individually.
        // This is required to soft delete all found entiries.
        foreach ($entities as $e) {
            $status = ($forceDelete) ? $e->forceDelete() : $e->delete();
            if ((bool) $status) {
                $deleteCount += 1;
            }
        }

        // Reset query builder
        $this->resetBuilder();

        return $deleteCount;
    }
public function deleteMorphsByEntity(IlluminateEloquentModel $entity, $relationName, $forceDelete = false)
    {
        // Retrieve all records by entity type and entity ID
        $entities = $this->getBuilder()
                         ->select(['id'])
                         ->where(function ($query) use ($entity, $relationName) {
                             $query->where($relationName.'_type', '=', get_class($entity))
                                   ->where($relationName.'_id', '=', (int) $entity->id);
                         })
                         ->get();

        // Delete count
        $deleteCount = 0;

        // Loop through each entity individually.
        // This is required to soft delete all found entiries.
        foreach ($entities as $e) {
            $status = ($forceDelete) ? $e->forceDelete() : $e->delete();
            if ((bool) $status) {
                $deleteCount += 1;
            }
        }

        // Reset query builder
        $this->resetBuilder();

        return $deleteCount;
    }
public function deleteMorphsByEntity ( IlluminateEloquentModel $ entity , $ relationName , $ forceDelete = false ) { // Retrieve all records by entity type and entity ID $ entities = $ this -> getBuilder ( ) -> select ( [ 'id' ] ) -> where ( function ( $ query ) use ( $ entity , $ relationName ) { $ query -> where ( $ relationName . '_type' , '=' , get_class ( $ entity ) ) -> where ( $ relationName . '_id' , '=' , ( int ) $ entity -> id ) ; } ) -> get ( ) ; // Delete count $ deleteCount = 0 ; // Loop through each entity individually. // This is required to soft delete all found entiries. foreach ( $ entities as $ e ) { $ status = ( $ forceDelete ) ? $ e -> forceDelete ( ) : $ e -> delete ( ) ; if ( ( bool ) $ status ) { $ deleteCount += 1 ; } } // Reset query builder $ this -> resetBuilder ( ) ; return $ deleteCount ; }
Delete morphed relations by entity.

Note: This should only be used with morphed relations

@author Morten Rugaard <moru@nodes.dk>

@param  \Illuminate\Database\Eloquent\Model $entity
@param  string                              $relationName
@param  bool                             $forceDelete
@return int
Delete morphed relations by entity .