2015-09-14 129 views
0

我在陣列試圖獲取非對象視圖中的屬性?

Array 
(
    [id] => 11 
    [username] => username 
    [avatar] => uploads/5.jpg 
    [email] => [email protected] 
    [designation] => TT 
    [supervisor] => Array 
     (
      [id] => 3 
      [username] => alex 
      [avatar] => uploads/3.jpg 
      [email] => [email protected] 
      [designation] => ADM 
      [supervisor] => 
      [created_at] => 2015-09-10 03:58:46 
      [updated_at] => 2015-09-10 03:58:46 
     ) 

    [created_at] => 2015-09-10 04:29:47 
    [updated_at] => 2015-09-10 04:29:47 
) 

我試圖訪問主管的用戶名,但它總是拋出這樣的錯誤

Trying to get property of non-object in view 

我曾經試圖訪問在以下方法follwing的數據,但它不」 t工作

$single_view_user->supervisor->username 


$single_view_user['supervisor']->username 

$single_view_user['supervisor']['username'] 

可以任何一個告訴我做錯了什麼? 謝謝

更新

在我打印的目的視圖

我使用

{{print_r($single_view_user->toArray())}} 

<tr class="active"> 
      <th>Profile Picture</th> 
       <td><img src="{{asset($single_view_user->avatar)}}" ></td> 
      </tr> 
      <tr class="active"> 
      <th>Username </th> 
       <td>{{$single_view_user->username}}</td> 
      </tr> 

      <tr class="active"> 
      <th>email</th> 
       <td>{{$single_view_user->email}}</td> 
      </tr> 
      <tr class="active"> 
      <th >Designation</th> 
       <td>{{$single_view_user->designation}}</td> 
      </tr> 
      <tr class="active"> 
      <th>supervisor</th> 
      <?php $single_view_user['supervisor']['username']; ?> 
       <td>{{ $single_view_user['supervisor']->username}}</td> 
      </tr> 

,並在我的控制器

$single_view_user = User::where('id',$id)->with('Supervisor')->first(); 

     return view('admin/single_view_user',['single_view_user'=>$single_view_user]); 

更新2

@foreach($single_view_user as $val) 


{{print_r($val) }} 

@endforeach 

輸出中會像

11 11 11 1 
+0

嘗試簡單地使用'$ single_view_user [ '監督員'] [ 'username']' –

+0

嗨,你能告訴我們你如何傳遞對象來查看?這很重要。 – Extrakun

+0

@vision錯誤? – aldrin27

回答

1

,它看起來像你的關係的方法是Supervisor,不supervisor(大寫小寫VS)。當您在模型上調用toArray時,它會通過Str::snake()方法運行關係屬性,這也會降低它們,這就是爲什麼Supervisor在數組輸出中顯示爲小寫字母的原因。

嘗試:

$single_view_user = User::where('id', $id)->with('Supervisor')->first(); 
dd($single_view_user->Supervisor->username); 

作爲一個側面說明,你可以清理你的查詢一點點:

$single_view_user = User::with('Supervisor')->find($id); 
+1

@ patricus.you救了我一堆時間 – iCoders

1

它應該是:

$single_view_user['supervisor']['username']; 

由於內部陣列是陣列不是對象。

在您的控制器中;

$single_view_user = $single_view_user = User::where('id',$id)->with('Supervisor')->first(); 

    $convert = $single_view_user->toArray(); 
    return view('admin/single_view_user',compact('convert',$convert)); 

在你看來:

<tr class="active"> 
     <th>Profile Picture</th> 
      <td><img src="{{asset($convert['avatar'])}}" ></td> 
     </tr> 
     <tr class="active"> 
     <th>Username </th> 
      <td>{{$convert['username']}}</td> 
     </tr> 

     <tr class="active"> 
     <th>email</th> 
      <td>{{$convert['email']}}</td> 
     </tr> 
     <tr class="active"> 
     <th >Designation</th> 
      <td>{{$convert['designation']}}</td> 
     </tr> 
     <tr class="active"> 
     <th>supervisor</th> 
     {{$convert['supervisor']['username']}} 
      <td>{{$convert['supervisor']['username']}}</td> 
     </tr> 
+0

嗨,我打印對象爲print_r($ single_view_user-> toArray()) – iCoders

+0

是的,因爲'toArray()'將所有的數據轉換爲數組。 – aldrin27

+0

ya.but not working – iCoders

1

使用下面的代碼:
echo $yourarray['supervisor']['username'];

->運算符用於訪問對象內部的財產,但你擁有的是一個數組。數組使用索引進行訪問。

編輯的零件答: 在您的視圖中使用下面的代碼:根據您的with()方法

<tr class="active"> 
      <th>Profile Picture</th> 
       <td><img src="{{asset($single_view_user['avatar'])}}" ></td> 
      </tr> 
      <tr class="active"> 
      <th>Username </th> 
       <td>{{$single_view_user['username']}}</td> 
      </tr> 

      <tr class="active"> 
      <th>email</th> 
       <td>{{$single_view_user['email']}}</td> 
      </tr> 
      <tr class="active"> 
      <th >Designation</th> 
       <td>{{$single_view_user['designation']}}</td> 
      </tr> 
      <tr class="active"> 
      <th>supervisor</th> 
      <?php $single_view_user['supervisor']['username']; ?> 
       <td>{{ $single_view_user['supervisor']['username']}}</td> 
      </tr> 
+0

是的,我已根據您在問題中的修改更新了答案。 –

+0

no.its not working.i將再次更新我的問題 – iCoders

+0

您所看到的錯誤信息是什麼? –

相關問題