2014-03-24 72 views
0

我的問題並不難:)我使用Laravel 4,我只想得到我的列表的第一個元素,我的代碼是:如何獲取laravel中的列表的第一個元素?

public function index() 
{ 
    $userid = User::lists('id'); // here i want to get the first element of my list 
    $services = User::find($userid)->service; 
    $username = User::lists('username'); 

    return View::make('services.index',array('services'=> $services,'username'=>$username)); 
} 

所以如果有人有任何想法我會非常感激:)

回答

1

在Laravel中,'lists'方法返回一個數組。所以,你可以簡單地通過使用類似訪問的第一個對象數組中:

$userid[0] = $firstUserId; 
1

如果您不需要名單只是第一個項目:

$user = User::first(); 

$userid = $user->id 
$services = $user->service; 
$username = $user->username; 
相關問題