1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Te7aHoudini\LaravelTrix\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Http\Response; |
7
|
|
|
use Illuminate\Routing\Controller; |
8
|
|
|
use Illuminate\Support\Facades\Storage; |
9
|
|
|
use Illuminate\Support\Facades\Validator; |
10
|
|
|
use Te7aHoudini\LaravelTrix\Models\TrixAttachment; |
11
|
|
|
|
12
|
|
|
class TrixAttachmentController extends Controller |
13
|
|
|
{ |
14
|
|
|
public function store(Request $request) |
15
|
|
|
{ |
16
|
|
|
$validator = Validator::make($request->all(), [ |
17
|
|
|
'file' => 'required|file', |
18
|
|
|
'modelClass' => 'required', |
19
|
|
|
'field' => 'required', |
20
|
|
|
]); |
21
|
|
|
|
22
|
|
|
if ($validator->fails()) { |
23
|
|
|
return response()->json(['errors'=>$validator->errors()], Response::HTTP_UNPROCESSABLE_ENTITY); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$attachment = $request->file->store('/', $request->disk ?? config('laravel-trix.storage_disk')); |
27
|
|
|
|
28
|
|
|
$url = Storage::disk($request->disk ?? config('laravel-trix.storage_disk'))->url($attachment); |
29
|
|
|
|
30
|
|
|
TrixAttachment::create([ |
31
|
|
|
'field' => $request->field, |
32
|
|
|
'attachable_type' => $request->modelClass, |
33
|
|
|
'attachment' => $attachment, |
34
|
|
|
'disk' => $request->disk ?? config('laravel-trix.storage_disk'), |
35
|
|
|
]); |
36
|
|
|
|
37
|
|
|
return response()->json(['url' => $url], Response::HTTP_CREATED); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function destroy($url) |
41
|
|
|
{ |
42
|
|
|
$attachment = TrixAttachment::where('attachment', basename($url))->first(); |
43
|
|
|
|
44
|
|
|
return response()->json(optional($attachment)->purge()); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: