Ludwig
Ludwig

Reputation: 1791

Laravel - delete whole collection

I have images for articles, and when I am updating article I would like to check if the images are the same, if not I would like to delete them but if it is possible I would like to delete the whole collection without another query, something like what I have in the code below $images->delete();. This is my function:

$images = Media::where('article_id', $article->id)->get();

    foreach($images as $image) {
        $article_images[] = $image->original_name;
    }

    foreach($files as $file) {
      $filePathArr = explode('/', $file);
      $fileName = array_pop($filePathArr);
      $originalFile = explode('-', $fileName);
      $originalFileName = array_pop($originalFile);
      $newFiles[] = $originalFileName;
    }

    if ($newFiles != $article_images){
      $images->delete();
    }

Upvotes: 34

Views: 59044

Answers (3)

Ntiyiso Rikhotso
Ntiyiso Rikhotso

Reputation: 720

I'd delete a whole collection by the doing following after calling Model::get() or Model::all()

$posts = Post::all();
// Logic
// More logic
$posts->map->delete();

Upvotes: 1

MCFreddie777
MCFreddie777

Reputation: 1213

If you have your Models linked, you can.

Class Exercise.php:

/**
 * Exercise belongs to exactly one lecture
 *
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */

public function lecture()
{
    return $this->belongsTo('\App\Lecture');
}

and Class Lecture.php:

/**
 * Gets all the exercises asociated to this lecture
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */

public function exercises()
{
    return $this->hasMany('\App\Exercise');
}

Then you can in your controller simply do:

public function delete($id, DeleteLectureRequest $request)
{
        $lecture = Lecture::findOrFail($id);
        $lecture->exercises()->delete();      // easy
}

(Imagine that your Article == my Lecture, and your Media == my Exerises)

Of course, at first you have to set properly foreign keys in your DB and link your Models that way.

Upvotes: 19

Sigismund
Sigismund

Reputation: 1082

You just can't delete from database without making a query.

You will have to make new request like this:

Media::where('article_id', $article->id)->delete();

It's just one simple query, so there shouldn't be any performance penalty.

If we are talking about collection with 100's of items, you can optimize the query like this:

Media::whereIn('id', $images->pluck('id'))->delete(); 

Upvotes: 64

Related Questions