Laravel-4-Bootstrap-Starter-Site icon indicating copy to clipboard operation
Laravel-4-Bootstrap-Starter-Site copied to clipboard

Error when running artisan db:seed

Open carnevalle opened this issue 11 years ago • 11 comments

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.

carnevalle avatar Jul 19 '13 19:07 carnevalle

Are you still getting this error?

andrew13 avatar Aug 02 '13 17:08 andrew13

I am having this error running the said command.

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

rifeman2007 avatar Aug 12 '13 02:08 rifeman2007

Didn't mean to close it :-)

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

carnevalle avatar Aug 12 '13 08:08 carnevalle

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

jenil avatar Feb 28 '14 12:02 jenil

Fresh install Same here...

RicardoRamirezR avatar Feb 28 '14 17:02 RicardoRamirezR

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;
    }
}

thiagomata avatar Apr 03 '14 13:04 thiagomata

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]

caiocutrim avatar Sep 19 '14 15:09 caiocutrim

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;        

jmichaelterenin avatar Oct 21 '14 22:10 jmichaelterenin

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

Gatix avatar Dec 01 '14 04:12 Gatix

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

slouma2000 avatar Dec 02 '14 04:12 slouma2000

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;
}

rebornishard avatar Apr 02 '15 15:04 rebornishard