Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error on delete when withFiles is enabled #12

Merged
merged 1 commit into from Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/CKEditor5Classic.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use NumaxLab\NovaCKEditor5Classic\Handlers\DiscardPendingAttachments;
use NumaxLab\NovaCKEditor5Classic\Handlers\StorePendingAttachment;
use NumaxLab\NovaCKEditor5Classic\Models\PendingAttachment;
use NumaxLab\NovaCKEditor5Classic\Models\DeleteAttachments;
use NumaxLab\NovaCKEditor5Classic\Models\DetachAttachment;

class CKEditor5Classic extends Trix
{
Expand Down Expand Up @@ -49,6 +51,8 @@ public function withFiles($disk = null)
$this->disk($disk);

$this->attach(new StorePendingAttachment($this))
->detach(new DetachAttachment($this))
->delete(new DeleteAttachments($this))
->discard(new DiscardPendingAttachments())
->prunable();

Expand Down
44 changes: 44 additions & 0 deletions src/Models/DeleteAttachments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace NumaxLab\NovaCKEditor5Classic\Models;

use Illuminate\Http\Request;

class DeleteAttachments
{
/**
* The field instance.
*
* @var \Laravel\Nova\Fields\Trix
*/
public $field;

/**
* Create a new class instance.
*
* @param \Laravel\Nova\Fields\Trix $field
* @return void
*/
public function __construct($field)
{
$this->field = $field;
}

/**
* Delete the attachments associated with the field.
*
* @param \Illuminate\Http\Request $request
* @param mixed $model
* @return void
*/
public function __invoke(Request $request, $model)
{
Attachment::where('attachable_type', get_class($model))
->where('attachable_id', $model->getKey())
->get()
->each
->purge();

return [$this->field->attribute => ''];
}
}
22 changes: 22 additions & 0 deletions src/Models/DetachAttachment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace NumaxLab\NovaCKEditor5Classic\Models;

use Illuminate\Http\Request;

class DetachAttachment
{
/**
* Delete an attachment from the field.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
public function __invoke(Request $request)
{
Attachment::where('url', $request->attachmentUrl)
->get()
->each
->purge();
}
}