0

我第一次尝试发送使用模型并遇到混乱。当我运行查询时,规则与之链接,应该是这样吗?

模型:

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

控制器

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

它打印出整个模型规则、消息、关系等逻辑。我做错了什么还是这正常?

4

1 回答 1

2

在您的has_role方法中,您正在返回User模型

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

所以,它正在转储User模型,并且它正在做正确的事情,正如它通过以下代码所做的那样

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

您可以使用而不是转储模型

$user = User::has_role('writer_en');
echo $user->firstname;
echo $user->lastname;
于 2013-10-07T18:37:49.687 回答