Titamik
Titamik

Reputation: 39

Uploading files to front end

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

Upvotes: 0

Views: 702

Answers (1)

Pettis Brandon
Pettis Brandon

Reputation: 865

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

Upvotes: 1

Related Questions