Closed aghayeff closed 5 years ago
Upload is not yet supported by default, you need to make your own handler for it.
protected $actions = ['create', 'edit', 'remove', 'upload'];
public function upload(Request $request) {
// handle upload request.
}
ohhh, I can't understand why I used action method instead of upload :) Thank you!
BTW, method upload always returns an error: A server error occurred while uploading the file
Also, can I send attached file ( $request->image) to creating method, to store it in another database (media ) ?
See this class I am using for uploading images. It's a WIP but working as expected for me.
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Spatie\Image\Image;
use Spatie\Image\Manipulations;
use Yajra\DataTables\DataTablesEditor;
abstract class DataTablesUploadEditor extends DataTablesEditor
{
protected $actions = ['create', 'edit', 'remove', 'upload'];
protected $uploadDir = '/storage/upload/';
protected $imageWidth = 500;
public function upload(Request $request)
{
try {
$request->validate($this->uploadRules());
$type = $request->input('uploadField');
$dir = $this->uploadDir . $type;
$uploadedFile = $request->file('upload');
$filename = time() . '.png';
$uploadedFile->move(public_path($dir), $filename);
$method = 'optimize' . Str::title(camel_case($type));
if (method_exists($this, $method)) {
$id = $this->{$method}($dir, $filename);
} else {
$id = $this->optimize($dir, $filename);
}
return response()->json([
'data' => [],
'upload' => [
'id' => $id,
],
]);
} catch (\Exception $exception) {
return response()->json([
'data' => [],
'fieldErrors' => [
[
'name' => $request->get('uploadField'),
'status' => $exception->getMessage(),
],
],
]);
}
}
public function uploadRules()
{
return [
'upload' => 'required|image',
];
}
protected function optimize($dir, $filename)
{
$path = public_path($dir . '/' . $filename);
Image::load($path)
->width($this->imageWidth)
->format(Manipulations::FORMAT_PNG)
->optimize()
->save();
return $dir . '/' . $filename;
}
}
Thank you. At the end, what about uploading file on submit? Sometimes, it is not useful uploading file immediately. Lets say that I want to build a simple form with image. I do not need any method for uploading image. Upload will be on create method.
P.S I use Service Implementation. All fields come from UsersDatatable.php. Cant make possible returning image preview.
I was thinking the same thing but that is not how the editor upload function work unfortunately. And afaik, there is no native support for direct upload upon submit or I might be missing something. The last time I checked, it was I think due to the ajax submission of form. If you find a way for that, please share your solution :)
This article is suitable, but I can't even figure out how it worked for him :)
Is there any documentation on how to use uploadMany? Upload seems to work but in this context I am getting the error ""Uncaught Upload collections must have an array as a value"
Fields\Image::make('photo_link2')->type('uploadMany')
Does anyone have a sample that they can share?
@scottsuhy sorry no docs yet. Use array on name when using uploadMany.
File::make('property_documents[]')->multiple()->label('Property Documents'),
@scottsuhy sorry no docs yet. Use array on name when using uploadMany.
File::make('property_documents[]')->multiple()->label('Property Documents'),
I didn't quite understand how to use this function, now I have to add two uploads, one for PDFs and one for images, even if I see the upload form when I try to upload it gives me this error "A server error occurred while uploading the file"
public function createRules(): array
{
return [
'categoria' => 'required',
'title' => 'required',
'description' => 'required',
'file' =>'required|mimes:pdf,xlx,xlsx,xls,csv,txt|max:10048',
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
];
}
->editors([
Editor::make()
->fields([
Fields\Text::make('categoria')->label('Fornitore'),
Fields\Text::make('title')->label('Nome'),
Fields\Text::make('description')->label('Anno'),
Fields\File::make('file_path')->label('Listino')->type('upload'),
Fields\Image::make('image_path')->label('Formato')->type('upload'),
]),
])
Where am I doing wrong?
Error: Requested action not supported!
I get this error, while trying upload a file using field type: upload
As I understand, on your code, you have default methods, such as create, edit, remove. I added extra action value into the array on my UsersDataTablesEditor Controller. Then, created an extra method for action. But, still get the same error.
Creating an action method for file upload, i just want to check validation, resize a photo and move to filesystem. After, clicking the submit button, file data should be uploaded to the database. But, cant figure out how to get it work.