OctoberCMS Builder plugin, uploading file and storing in database

南笙酒味 提交于 2019-12-02 00:04:32

Ok guys, eventually i have solved this by following these steps.

First of all, we do not need to create any column in database table to store any file upload names in any field in database table as OctoberCMS will automatically in system_files table. So i simply removed 'social_logo' field via builder plugin.

Then i removed old file upload control from my current Social Links plugin using Builder plugin from admin and simply created new one without setting up any relation with any table's field.

Then simply i went to Sociallinks.php model,and made sure this code is there

public $attachOne = [
            'social_logo' => 'System\Models\File'
    ];

Also make sure your mode should be image and your type can be fileupload as i am able to see images in edit record section like this .. you can update this code though based on your needs but this works for me well so i put this code...

fields.yaml

fields:
    social_logo:
        label: 'Social Logo'
        mode: image
        span: auto
        type: fileupload

and then i went to admin area and created test template and in Code tab i put this code just to check single record for now,

use technobrave\sociallinks\Models\Sociallink;


function onStart()
{
    $this['model_data'] = Sociallink::first();
}

and in template markup, i simply put this

<img src="{{ model_data.social_logo.getPath() }}" />

and whoa, i can see image .. :)

Additionaly, if you want multiple data then you can put this code in Code Tab,

use technobrave\sociallinks\Models\Sociallink;


function onStart()
{
    $this['model_data'] = Sociallink::all();
}

And your html markup tab will contain following code,

<h2>Image List</h2>
<ul>
    {% for current_model_data in model_data %}
    <h3>{{ current_model_data.id }}</h3>
    <img src="{{ current_model_data.social_logo.getPath() }}" />


    {% endfor %}
</ul>

Thanks for support guys .. hope this helps to someone who can be new to OctoberCMS.. highly appreciated ..

Thank you ..

trollkotze

If it is necessary for you to have the file name stored in the database, the best way might be to use the Media Finder Widget instead of the File Upload Widget, and remove the attachOne relationship from your model.

  • The MediaFinder widget lets you store files anywhere within the storage/media subdirectory of your OctoberCMS installation, choosing the exact storage path therein that you want, and simply returns the stored file path as a string - that you then need to store as a column in the database record for your model, just as you seem to have had in mind.

  • The File upload widget, combined with the attachOne (or attachMany) relationship works much differently. The attachOne (or attachMany) relationship does not correspond to an actual database column for your model. The information which files are attached to what other models here or there is stored and retrieved elsewhere - how exactly, I don't know. (The information about uploaded files through the File upload widget and their storage paths (which are generated randomly with no user control) are stored in the system_files database table - how the association is made to the parent models where the files are attached, I have no clue.)

You already have a file name column in your database table, and it seems that you would like your file names to be somehow "nice" and readable and perhaps under user control. So I would suggest that you simply change your fields.yaml like so:

fields:
    social_logo:
        label: 'technobrave.sociallinks::lang.Sociallink.social_logo'
        span: auto
        oc.commentPosition: ''
        type: mediafinder
        thumbOptions:
            mode: crop
            extension: auto

(I don't know if the thumbOptions settings work here. You can try and see. As for the useCaption it can not be used here, since files in the media library are simply referenced by strings and have no corresponding eloquent model with additional information like title and description, as the File Upload files do.)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!