CodeSleeve / laravel-stapler

Stapler-based file upload package for the Laravel framework.
MIT License
556 stars 109 forks source link

Auth::user()->avatar->url() is undefined #79

Closed publicarray closed 9 years ago

publicarray commented 9 years ago

First of all, thank you for this awesome repo!

Previously in Laravel 4 I could get the current user's profile image using: {{ asset(Auth::user()->image->url('thumb')) }} I have updated Laravel to version 5 and now the image object appears to be undefined:

Call to a member function url() on null

The problem in context: layout.blade.php

Edit I worked around the issue by creating the url with str_pad () and chunk_split() functions.

tabennett commented 9 years ago

If you have an attachment defined on the user model (named 'image'), that should be working. Are you using the correct service provide for L5? Also, have you started using any other packages that might be intercepting the call to getAttribute() on the User model?

If you don't mind showing me some code (your User model, etc) I'll try and help you work through this.

publicarray commented 9 years ago

Thanks,

Start of my User model:

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Codesleeve\Stapler\ORM\StaplerableInterface;
use Codesleeve\Stapler\ORM\EloquentTrait;

class User extends Eloquent implements AuthenticatableContract, CanResetPasswordContract, StaplerableInterface {
    use EloquentTrait, Authenticatable, CanResetPassword;

    /**
    * The stapler constructor -> specifies image dimensions and styles
    *
    *
    */
    public function __construct(array $attributes = array()) {
        $this->hasAttachedFile('image', [
            'styles' => [
                'medium' => '300',
                'thumb' => '100x100#',
                ]
        ]);

        parent::__construct($attributes);
    }

    public function seeker()
    {
        return $this->hasOne('Seeker');
    }

    public function employer()
    {
        return $this->hasOne('Employer');
    }
// ...

In my app.php I am using L5 service provider

'Codesleeve\LaravelStapler\Providers\L5ServiceProvider',

And I'm my composer.json I am using the dev-master version of laravel-stapler:

"require": {
    "php": ">=5.5.9",
    "laravel/framework": "5.1.*",
    "codesleeve/laravel-stapler": "dev-master"
},

The migration file is brought forward from Laravel 4 after a php artisan stapler:fasten users image

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class AddImageFieldsToUsersTable extends Migration {
    /**
     * Make changes to the table.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function(Blueprint $table) {
            $table->string("image_file_name")->nullable();
            $table->integer("image_file_size")->nullable();
            $table->string("image_content_type")->nullable();
            $table->timestamp("image_updated_at")->nullable();
        });
    }
// ...

And in my seed file I create a new user:

// ...
        $user = new User;
        $user->name = 'Coles';
        $user->username = 'coles';
        $user->email = 'Coles@Coles.com';
        $user->phone = '98765432';
        $user->role = 'employer';
        $user->password = Hash::make('123456');
        $user->image_file_name = 'coles.jpg';
        $user->image_content_type = 'image/jpeg';
        $user->save();
// ...

In this project I am not using any other packages.

Edited to add relationships to user.php

also {{ asset($user->image->url('thumb')) }} works fine.

publicarray commented 9 years ago

In Laravel 5 there is a new file in laravel/app called User.php, which seems to do the Authentication. Maybe this is part of the problem?

tabennett commented 9 years ago

If you do:

$user = Auth::user()
$user->image->url('thumb');

Do you still get that error?

To be honest, I haven't used Auth::User() in a long time, so I don't know if anything changed about how the fluent interface works or not. But I don't think this is an issue with Stapler.

publicarray commented 9 years ago

Unfortunately yes, Thanks for your help though. I think, I will post an issue to the Laravel repo. for me its's not that important as I worked around the problem. (I know it's not an elegant solution though)

Edit: In the Laravel Release Notes there are a few mentions on changes with authentication. So it's probably best to ask them how that impacts adding objects to Auth::User();