Ad

Uploading Files To Front End

- 1 answer

I use the User plugin. I used migrations

    public function up()
    {
        Schema::table('users', function($table)
        {

            $table->text('sex')->nullable();
            $table->date('birthday')->nullable();
            $table->string('doc_photo')->nullable();
        });
    }

Made the fields editable both in the admin panel and in the front-end User account

        UserModel::extend(function($model){
            $model->addFillable([
                'sex',
                'birthday',
                'doc_photo'
            ]);
        });

        UsersController::extendFormFields(function($form, $model, $context){
            $form->addTabFields([
                'sex' => [
                    'label' => 'Sex',
                    'type' => 'text',
                    'tab' => 'Personal info'
                ],              
                'birthday' => [
                    'label' => 'Birthday',
                    'type' => 'text',
                    'tab' => 'Personal info'
                ]
                'doc_photo' => [
                    'label' => 'Photo',
                    'type' => 'text',
                    'tab' => 'Documents'
                ]
            ]);
        });

How do I use to make a file upload to the server. For example, avatar

Ad

Answer

Here is how I did it for the backend:

Remove doc_photo from database table

public function up()
{
    Schema::table('users', function($table)
    {

        $table->text('sex')->nullable();
        $table->date('birthday')->nullable();
    });
}

Plugin.php - add doc_photo as an attachment not a field and as a fileupload widget in backend controller

    UserModel::extend(function($model){
        $model->addFillable([
            'sex',
            'birthday'
        ]);
        $model->attachOne['doc_photo'] = 'System\Models\File';
    });

    UsersController::extendFormFields(function($form, $model, $context){
        $form->addTabFields([
            'sex' => [
                'label' => 'Sex',
                'type' => 'text',
                'tab' => 'Personal info'
            ],              
            'birthday' => [
                'label' => 'Birthday',
                'type' => 'text',
                'tab' => 'Personal info'
            ],
            'doc_photo' => [
                'label' => 'Photo',
                'type' => 'fileupload',
                'mode' => file,
                'thumbOptions' => [
                    'mode' => 'crop',
                    'extension' => 'auto'
                ],
                'useCaption' => true,
                'tab' => 'Documents'
            ]
        ]);
    });

This is for the front end:

Make a component that has an onUpload() function and load Auth facade:

public function onUpload() {
    $user = Auth::getUser();
    $user->doc_photo = Input::file("doc_photo");
    $user->save();

    return Redirect::refresh();
}

HTML/Twig Code to find in the default.htm partial:

{{ form_open({files: true, request: 'onUpload'}) }} 
    <!--File Input--> 
    <input type="file" name="doc_photo"  required="required" class=""> 
    <!--File Input--> 
    <!--Submit/Upload Button--> 
    <button type="submit" class="">Upload</button> 
 {{ form_close() }}
Ad
source: stackoverflow.com
Ad