Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when running artisan db:seed #168

Open
carnevalle opened this issue Jul 19, 2013 · 11 comments
Open

Error when running artisan db:seed #168

carnevalle opened this issue Jul 19, 2013 · 11 comments

Comments

@carnevalle
Copy link

When I run php artisan db:seed i get the following error message:

{"error":{"type":"ErrorException","message":"call_user_func_array() expects parameter 1 to be a valid callback, non-static method Role::beforeSave() should not be called statically","file":"/Users/me/Web/project/bootstrap/compiled.php","line":5233}}

I am completely new to Laravel, so don't know how and where to debug what is going on. Is it perhaps something with the validation of the model?

I can get it working if i substitute the content of RolesTableSeeder with

        DB::table('roles')->delete();

        $roles = array(
            array(
                'name'      => 'admin',
                'created_at' => new DateTime,
                'updated_at' => new DateTime,
            ),
            array(
                'name'      => 'comment',
                'created_at' => new DateTime,
                'updated_at' => new DateTime,
            )
        );

        DB::table('roles')->insert( $roles );

        $user = User::where('username','=','admin')->first();
        $user->attachRole( Role::where('name','=','admin')->first() );

        $user = User::where('username','=','user')->first();
        $user->attachRole( Role::where('name','=','comment')->first() );

There is another problem, that you can't execute db:seed multiple times, as some ID's are hardcoded and running multiple db:seed will empty the table but not reset the incrementing ID's. To resolve this issue wipe the database and migrate before seeding.

@andrew13
Copy link
Collaborator

andrew13 commented Aug 2, 2013

Are you still getting this error?

@rifeman2007
Copy link

I am having this error running the said command.

[BadMethodCallException]
Call to undefined method Illuminate\Database\Query\Builder::attachRole()

@carnevalle
Copy link
Author

Didn't mean to close it :-)

I have tried once again with no problems. So don't know what caused the problem.

@carnevalle carnevalle reopened this Aug 12, 2013
@jenil
Copy link

jenil commented Feb 28, 2014

Hey guys,

I too am getting this error like @carnevalle said. I am not sure why is it coming up. Any solution for this?

PHP Fatal error: Call to undefined method stdClass::attachRole() in /Users/jenil/Sites/esperanto/app/database/seeds/RolesTableSeeder.php on line 19 {"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to undefined method stdClass::attachRole()","file":"\/Users\/jenil\/Sites\/esperanto\/app\/database\/seeds\/RolesTableSeeder.php","line":19}}{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to undefined method stdClass::attachRole()","file":"\/Users\/jenil\/Sites\/esperanto\/app\/database\/seeds\/RolesTableSeeder.php","line":19}}

@RicardoRamirezR
Copy link

Fresh install
Same here...

@thiagomata
Copy link

This is not the exactly the same problem. But it is a very close error to open a new issues. Take a look:

$ php artisan db:seed
PHP Fatal error:  Trait 'Zizaco\Entrust\HasRole' not found in /vagrant/laravel/app/models/User.php on line 7
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Trait 'Zizaco\\Entrust\\HasRole' not found","file":"\/vagrant\/laravel\/app\/models\/User.php","line":7}}{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Trait 'Zizaco\\Entrust\\HasRole' not found","file":"\/vagrant\/laravel\/app\/models\/User.php","line":7}}
$ cat composer.json
{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
        "laravel/framework": "4.1.*",
        "zizaco/confide": "dev-master",
        "intervention/image": "dev-master",
        "google/apiclient": "dev-master"
    },
    "autoload": {
        "classmap": [
            "app/commands",
            "app/controllers",
            "app/models",
            "app/database/migrations",
            "app/database/seeds",
            "app/tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-install-cmd": [
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ]
    },
    "config": {
        "preferred-install": "dist"
    },
    "minimum-stability": "dev"
}
$ cat ./app/models/User.php 
<?php

use Zizaco\Confide\ConfideUser;
use Zizaco\Entrust\HasRole;

class User extends ConfideUser {
    use HasRole;

    public $errors;

    protected $table = 'user';
    protected $softDelete = true;
    protected $fillable = array('access_token', 'refresh_token');

    public $skipValidations = false;

    /**
     * Validation rules
     */
    public static $rules = array(
        'email' => 'unique:users|required|email',
        'password' => 'required|between:4,11|confirmed',
    );

    public static function boot() {
        parent::boot();

        static::saving(function($model) {
            return $model->validate();
        });
    }

    public function delete() {
        $this->roles()->detach();

        return parent::delete();
    }

    public function name() {
        $arr = explode( '@', $this->email );
        return array_shift( $arr );
    }

    public function setAccessToken($token) {
        $this->skipValidations = true;

        return $this->update(array(
            'access_token' => $token,
        ));
    }

    public function validate(array $rules = array(), array $customMessages = array()) {
        if ($this->skipValidations) return true;

        $validator = Validator::make($this->attributes, static::$rules);

        if ($validator->passes()) return true;

        $this->errors = $validator->messages();

        return false;
    }
}

@caiocutrim
Copy link

me too with this same issue
{"error":{"type":"ErrorException","message":"parse_url() expects parameter 1 to be string, array given","file":"/var/www/www.magazine.dev/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Request.php","line":321}}[Finished in 0.1s]

@jmichaelterenin
Copy link

Call to undefined method stdClass::attachRole()

I'm not completely new to Laravel (v4.2.*) and definitely not to PHP (PHP v5.4.19), so I understand what's going on here, but I can't get this to work:

$admin = DB::table('roles')->where('name', '=', 'Admin')->pluck('id');      
$user = DB::table('users')->where('id', '=', '1')->first();             
/* role attach alias */
$user->attachRole( $admin );

models/user.php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
use Zizaco\Entrust\HasRole;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait, HasRole;        

@Gatix
Copy link

Gatix commented Dec 1, 2014

You probably have a role relation in your User model that overrides the HasRole's role relation method. Remove it and it will work.

@slouma2000
Copy link

I have the same Error

PHP Fatal error:  Call to a member function attachRole() on null in /var/www/b4x/app/database/seeds/RolesTableSeeder.php on line 18

I haven't an override for HasRole method

@rebornishard
Copy link

if u combine confide with zicaco

see your user model

<?php

use Zizaco\Confide\ConfideUser;
use Zizaco\Confide\ConfideUserInterface;
use Zizaco\Entrust\HasRole;

class User extends Eloquent implements ConfideUserInterface
{
    use ConfideUser;
    use HasRole;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests