This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Shahiem
Shahiem

I'm busy with an avatar plugin but I get this error when I try to upload a new image.

unexpected type of array, should attribute data be jsonable.

Code part

$model = User::find(3);

$file = new System\Models\File;
$file->data = Input::file('dsd');
$file->save();

$model->avatar()->add($file);

How can I solve this issue? Thank you :)

Shahiem

Last updated

jamiegrand
jamiegrand

I did it like this...

use System\Models\File as myFile;

public function onAvatar()
{
    $this->user = $this->page['user'] = $this->user();
    $file = new myFile;
    $file->data = Input::file('avatar');
    $file->save();
    $this->user->avatar()->add($file);

}

and the form in the front end

{{ form_open({ request: 'onAvatar' , files: true  }) }}
    <div class="form-group"> 
        <label for="avatar">Change Image:</label>
        <input type="file" name="avatar" class="filestyle" data-buttonName="btn-primary" required>
    </div>
    <div class="form-group">
        <button class="pull-right btn  btn-primary" type="submit">Submit</button>
    </div>
{{ form_close() }}

to get the user

 /**
 * Returns the logged in user, if available
 */
public function user()
{
    if (!Auth::check())
        return null;

    return Auth::getUser();
}

Last updated

Shahiem
Shahiem

Thanks man, I'll check it out

Edit: Nice, your code works. Well done, thank you :)

Last updated

sulaz1x
sulaz1x

Thanks ! It's work also for me on dev environment, just a small bug while running on apache server (Prod), the form action url is miss written if octber is installed in a subfolder, you might need to add :

    $this->page['url'] = Request::url() ."/";

and :

    {{form_open({request: XYZ, 'url': url)} }}

to have an action link ending with a slash ( " / " ) refer to : https://github.com/octobercms/october/issues/634

Last updated

1-4 of 4

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.