diff --git a/resources/views/trixassets.blade.php b/resources/views/trixassets.blade.php index 0170bf4..2bc7cb7 100644 --- a/resources/views/trixassets.blade.php +++ b/resources/views/trixassets.blade.php @@ -113,6 +113,10 @@ function createFormData(data) { formData.append("disk", data.disk); } + if(data.path != undefined) { + formData.append("path", data.path); + } + return formData; } diff --git a/src/Http/Controllers/TrixAttachmentController.php b/src/Http/Controllers/TrixAttachmentController.php index 7408b6f..3ae1b88 100644 --- a/src/Http/Controllers/TrixAttachmentController.php +++ b/src/Http/Controllers/TrixAttachmentController.php @@ -23,7 +23,7 @@ public function store(Request $request) return response()->json(['errors'=>$validator->errors()], Response::HTTP_UNPROCESSABLE_ENTITY); } - $attachment = $request->file->store('/', $request->disk ?? config('laravel-trix.storage_disk')); + $attachment = $request->file->store($request->path ?? '/', $request->disk ?? config('laravel-trix.storage_disk')); $url = Storage::disk($request->disk ?? config('laravel-trix.storage_disk'))->url($attachment); @@ -39,7 +39,7 @@ public function store(Request $request) public function destroy($url) { - $attachment = TrixAttachment::where('attachment', basename($url))->first(); + $attachment = TrixAttachment::where('attachment', 'LIKE', '%'.basename($url))->first(); return response()->json(optional($attachment)->purge()); } diff --git a/src/Traits/HasTrixRichText.php b/src/Traits/HasTrixRichText.php index 6d8e1c2..035c68d 100644 --- a/src/Traits/HasTrixRichText.php +++ b/src/Traits/HasTrixRichText.php @@ -39,11 +39,15 @@ public static function bootHasTrixRichText() $attachments = Arr::get($model->savedAttachments, $field, []); - TrixAttachment::whereIn('attachment', is_string($attachments) ? json_decode($attachments) : $attachments) - ->update([ - 'is_pending' => 0, - 'attachable_id' => $model->id, - ]); + $attachments = is_string($attachments) ? json_decode($attachments) : $attachments; + + foreach ($attachments as $attachment) { + TrixAttachment::where('attachment', 'LIKE', "%$attachment") + ->update([ + 'is_pending' => 0, + 'attachable_id' => $model->id, + ]); + } } $model->savedTrixFields = []; diff --git a/tests/Feature/TrixAttachmentControllerTest.php b/tests/Feature/TrixAttachmentControllerTest.php index 52ddbf9..a81f347 100644 --- a/tests/Feature/TrixAttachmentControllerTest.php +++ b/tests/Feature/TrixAttachmentControllerTest.php @@ -23,9 +23,10 @@ public function it_can_store_attachment_request() 'modelClass' => Post::class, 'field' => 'fooField', 'disk' => 'fooDisk', + 'path' => 'fooPath', ]); $this->assertTrue( - TrixAttachment::where('attachment', basename($response->decodeResponseJson()->json('url'))) + TrixAttachment::where('attachment', 'LIKE', '%'.basename($response->decodeResponseJson()->json('url'))) ->where('is_pending', 1) ->exists() );