Search code examples
phplaravellaravel-3

Laravel 3 - Eloquent query returns rules


I'm trying to send use models for the first time and running into a confusion. When I run a query, the rules are linked with it, is it supposed to be like that?

Model:

class User extends Elegant
{
    public static $table = 'users';

    protected $rules = array(
        'email' => 'required|email',
        'firstname' => 'required',
        'lastname' => 'required',
        'initials' => 'required|alpha|match:/[A-Z]+/',
        'role' => 'required|in:writer_fr,writer_en,reader',
        'password' => 'min:6,max:32|same:password2'
    );

    public static function has_role($role)
    {
        //$u = new User;
        $users = User::where($role, '=', 1)->get(array('firstname', 'lastname'));
        return $users;
    }
}

Controller

$u = array();
$u['writer_en'] = User::has_role('writer_en');
dd($u['writer_en']);

Which prints out the entire model rules, messages, relationship etc logic. Am I doing something wrong or is this normal?


Solution

  • In your has_role method you are returning User model

    public static function has_role($role)
    {
        //$u = new User;
        $users = User::where($role, '=', 1)->get(array('firstname', 'lastname'));
        return $users; // <-- User model
    }
    

    So, it's dumping the User model and it's doing the right thing as it suppose to do by following code

    $u = array();
    $u['writer_en'] = User::has_role('writer_en');
    dd($u['writer_en']);
    

    Instead of dumping the model, you can use

    $user = User::has_role('writer_en');
    echo $user->firstname;
    echo $user->lastname;