2013-10-07 126 views
0

我試圖第一次發送使用模型,並陷入混亂。當我運行查詢時,規則與它鏈接,是否應該是這樣?Laravel 3 - 雄辯查詢返回規則

型號:

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']); 

打印出整個模型的規則,消息關係等的邏輯。我做錯了什麼或這是正常的?

回答

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; 
+0

確定謝謝你,我只是想確保執行整個用戶邏輯是正常的:) – veksen

+0

不客氣:-) –

+0

並將整個變量傳遞給您建議反對的視圖,或者我應該準備它第一? – veksen