5

I'm using Laravel 5.3 and I'm trying to remove files from a user within a job::

public function handle()
{
    //Remove all files from a message
    $this->files->map(function($file) {
        $path = $file->getPath();

        if(Storage::disk('s3')->exists($path))
        {
            Storage::disk('s3')->delete($path);
            if(!Storage::disk('s3')->exists($path))
            {
                $attachment = File::find($file->id);
                $attachment->delete();
            }
        }
    });
}

So this is working for collections. But how do I get this to work when I pass one model instance?

0

2 Answers 2

8

You can make it possible in different ways. You can check if $this->filies

if($this->files instanceof Illuminate\Database\Eloquent\Collection) {
  //so its a collection of files
} else {
  //its a one model instance
//here you can do hack, 
  $this->files = collect([$this->files]);
  //and code will works like a magic
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sure, you can just check if $this->files if(! ($this->files instanceof Illuminate\Database\Eloquent\Collection)) { $this->files = collect([$this->files]); }
1

First, as the algorithm you want apply to the collection's elements or the Eloquent model is the same, move it in a private method as follows:

private _removeFilesFromMessage($file) {
    $path = $file->getPath();

    if(Storage::disk('s3')->exists($path))
    {
        Storage::disk('s3')->delete($path);
        if(!Storage::disk('s3')->exists($path))
        {
            $attachment = File::find($file->id);
            $attachment->delete();
        }
    }
}

Then modify the handle method like this:

public function handle()
{
    if($this->files instanceof Illuminate\Database\Eloquent\Collection) {
        //Remove all files from a message
        $this->files->map($this->_removeFilesFromMessage($file));
    } else {
        $this->_removeFilesFromMessage($this->files);
    }
}

What are we doing here? We're checking if the $this->files instance is an Eloquent Collection and, in case the condition is true, we use the _removeFilesFromMessage as a callback for the map method. Otherwise (I am assuming that the $this->files contains an Eloquent Model instance) the _removeFilesFromMessage method is called and the model is passed.

I think this code is a good start for your needings.

edit

As the title of this question is partially different from what you have asked... for a matter of completion:

You can create Laravel Collections with the collect() method, as described in the Laravel 5.3 official doc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.