gonziis - 2 months ago

I 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.

ryanthompson - 2 months ago

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:

// The manager is the mount manager I inject in the construct
use use League\Flysystem\MountManager;

// This part basically puts a file - even if it exists (it'll update)
$source = 'http://figgeartmuseum.org' . $row->filter('img')->first()->attr('src');

$basename = basename($source);
$filename = substr($basename, 0, strpos($basename, '.aspx')) . '.jpg';

// This little part puts the file to filesystem and returns a file interface from files module to work with.
// You can take this and sync it to a pivot table relation or whatever. This is a single file FT so I just set the attribute.
$item['image'] = $this->files->put(
    'local://collections/' . $filename,
    file_get_contents($source)
);

// And later the item is inserted
$this->items->create($item);

ryanthompson - 2 months ago

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)
);

gonziis - 2 months ago

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))

ryanthompson - 2 months ago

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.

gonziis - 2 months ago

Hmm, the error is saying something about parent_id being null/empty. I did add the extension and that wasn't it. What kind of parent_id is it talking about? How can I even pass it to the method?

gonziis - 2 months ago

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?

gonziis - 2 months ago

Figured it out myself $report->Files()->syncWithoutDetaching($file->id); for each uploaded file.

ryanthompson - 2 months ago

Nicely done!