2016-05-12 73 views
0

我在數據庫中有一個通知表,並且還有一個用戶表。在通知表中,我有user_id,notific_user_id,post_id.I可以在Controller中打印$ r用戶變量,但可以訪問$ user->名字。laravel多維數組

這是控制器:

public function test() 
{ 
    $result=Notifications::where('user_id',Auth::user()->id)->get(); 
    echo '<pre>'; 


    foreach ($result as $result){ 
     $user=User::where('id',$result->notific_user_id)->first()->get(); 
     print_r($user); 
    } 
} 

這是一些輸出

Illuminate\Database\Eloquent\Collection Object 
(
[items:protected] => Array 
    (
     [0] => App\User Object 
      (
       [connection:protected] => 
       [table:protected] => 
       [primaryKey:protected] => id 
       [perPage:protected] => 15 
       [incrementing] => 1 
       [timestamps] => 1 
       [attributes:protected] => Array 
        (
         [id] => 100000000 
         [created_at] => 2016-04-28 19:25:01 
         [updated_at] => 2016-05-10 05:03:22 
         [user_name] => 1 
         [first_name] => Tuhin 
         [last_name] => Hossain 
         [nick_name] => 
         [email] => [email protected] 
         [password] => $2y$10$XtUWwJwVe2qf86CSUN6AkOkQlCWYq8/b3TrvBt2xuMlh4wTzRgxbG 
         [date_of_birth] => 2016-04-30 
         [gender] => 1 
         [remember_token] => a1U4V4rgJogw3hzXE9jJ8kHqtVrR5FkrMlvLEaRsC6qtmgX3LAbB3MkrZAgz 
        ) 

       [original:protected] => Array 
        (
         [id] => 100000000 
         [created_at] => 2016-04-28 19:25:01 
         [updated_at] => 2016-05-10 05:03:22 
         [user_name] => 1 
         [first_name] => Tuhin 
         [last_name] => Hossain 
         [nick_name] => 
         [email] => [email protected] 
         [password] => $2y$10$XtUWwJwVe2qf86CSUN6AkOkQlCWYq8/b3TrvBt2xuMlh4wTzRgxbG 
         [date_of_birth] => 2016-04-30 
         [gender] => 1 
         [remember_token] => a1U4V4rgJogw3hzXE9jJ8kHqtVrR5FkrMlvLEaRsC6qtmgX3LAbB3MkrZAgz 
        ) 

       [relations:protected] => Array 
        (
        ) 

的現在,我怎麼能訪問名字,姓氏等? 我試過print_r($user->first_name);但它沒有工作。請幫幫我。

回答

1

你得到一個集合(對象的列表),因爲你使用->get()方法爲您的口才查詢生成器

簡單地去掉 - > get()方法,你會得到的第一個對象匹配查詢

您的代碼應該看起來像這樣:

$user=User::where('id',$result->notific_user_id)->first(); 
+0

哦謝謝它的工作原理 –