Help uploading files from client side and putting into stream.
Created 6 years ago by gonziisI have stream with a field files
, type anomaly.field_type.files
. Also I have a form in the front-end with multiple file field, everything works, but I'm not sure how to actually upload the files and assign them to the stream. die(var_dump($request->file("files")));
in my controller after submitting actually gives me Illuminate\HttpUploadedFile
objects, so that's fine. But I cant find any direct examples how to upload files and assign them to streams, also the default PyroCMS Files module messes with my head. Can you give me some short, but direct explanation? I assume it shouldn't be so complicated.
This is the important part. The files is the mount manager as mentioned. Using this or laravel storage system will auto-sync to files module and also return file instances where applicable.
$this->files->put(
'local://collections/' . $filename,
file_get_contents($source)
);
I don't really understand this method. Why is there a topic about this? https://pyrocms.com/documentation/files-module/2.4/integration/laravel-filesystem but you recommend me using something else? Im too old for this framework automatication. I imagine something simple like - uploading a file to the local storage in a specific folder which I created in Adminpanel, then referencing that uploaded file to a stream field.
Also I tried just a simple Storage::disk('local')->put('parkapumu_dokumenti/file',$request->file("files")[0]);
But it gave me an error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'parent_id' in 'field list' (SQL: insert into `default_files_folders` (`disk_id`, `parent_id`, `created_at`, `created_by_id`, `sort_order`, `slug`) values (1, 4, 2018-06-06 16:08:57, 1, 5, file))
That will do it - and essentially what I showed you but I made the mistake of including a bunch of other crap lol.
However it thinks you are trying to make a folder because of the naming. Your put method needs to include the extension:
Storage::disk('local')->put('parkapumu_dokumenti/file.jpg',$request->file("files")[0]);
You can use the file name from the request object too I think.
Ok, thanks, the problem solved, just took out disk('local')
and used straight put
, now it uploads fine. But I need some help assigning the uploaded files to the stream.
$files = $request->file("files");
if(!empty($files)){
foreach ($files as $key => $value) {
Storage::put('dokumenti/'.$value->hashName(),file_get_contents($value));
}
}
$report = $reports->create([
"field1" => $q1,
"field2" => $q2,
"field3" => $saskare,
"field4" => $q4,
"field5" => $q5,
"field6" => $q6
]);
There is a field files
, how do I assign each uploaded file to the $report
?
Nicely done!
Generally speaking I use the
MountManager
or storage manager in Laravel and upload to disk / folder. Which returns a file instance and you can sync to a relation or set an attribute for singular file relations. Here is a code snippet I am using elsewhere in a project: