andrewelkins / Laravel-4-Bootstrap-Starter-Site

Laravel 4 Starter Site is a basic blog application using several nice community packages.
1.77k stars 603 forks source link

Error when running artisan db:seed #168

Open carnevalle opened 11 years ago

carnevalle commented 11 years ago

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 commented 11 years ago

Are you still getting this error?

rifeman2007 commented 11 years ago

I am having this error running the said command.

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

carnevalle commented 11 years ago

Didn't mean to close it :-)

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

jenil commented 10 years ago

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 commented 10 years ago

Fresh install Same here...

thiagomata commented 10 years ago

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 commented 10 years ago

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 commented 10 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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