2015-10-07 35 views
1

我有一個變量傳遞雖然我在我的視圖控制器: 名字是$其他如何laravel訪問集合的單值屬性5

當我做一個foreach和打印它像

@foreach($others as $other) 
    {{ $other }} 
@endforeach 

輸出爲:

[{"employeeID":"9125123981003","email":"[email protected]","fullName":"Pranshu Jain"}]  [{"employeeID":"45755757577","email":"[email protected]","fullName":"Pranshu Jain"}] 

我想要的就是訪問從這些單屬性?他們是收藏品嗎?

我試過這樣

@foreach($others as $other) 
    {{ $other->email }} 
    @endforeach 

這是我如何得到這個$others填充:

public function show($id) 
    { 
    $this->data['leave_application'] = Attendance::find($id); 
    $date_applied_on = $this->data['leave_application']->date; 
    $emp_id = $this->data['leave_application']->employeeID; 
    $other_on_leave = Attendance::where('date', '=', $date_applied_on)->get(); 
    $employees_on_leave = []; 

    foreach($other_on_leave as $others) { 
     if($emp_id != $others->employeeID) { 
      $emptyarray = Employee::where('employeeID', '=', $others->employeeID)->get(array('employeeID','email','fullName')); 

      $employees_on_leave[] = $emptyarray; 
     } 
    } 


    $this->data['others'] = $employees_on_leave; 
    return view('admin.leave_applications.show', $this->data); 
} 

但它不工作。請幫助和引導一點點。我熟悉數組和對象。我曾經與他們做得很好。但這似乎並不複雜。

,如果我做DD($等),然後我得到

   array:2 [▼ 
       0 => Collection {#344 ▼ 
       #items: array:1 [▼ 
        0 => Employee {#345 ▼ 
        #guarded: array:1 [▼ 
         0 => "id" 
        ] 
        #hidden: array:1 [▼ 
         0 => "password" 
        ] 
        #connection: null 
        #table: null 
        #primaryKey: "id" 
        #perPage: 15 
        +incrementing: true 
        +timestamps: true 
        #attributes: array:3 [▶] 
        #original: array:3 [▶] 
        #relations: [] 
        #visible: [] 
        #appends: [] 
        #fillable: [] 
        #dates: [] 
        #dateFormat: null 
        #casts: [] 
        #touches: [] 
        #observables: [] 
        #with: [] 
        #morphClass: null 
        +exists: true 
        +wasRecentlyCreated: false 
        } 
       ] 
       } 
       1 => Collection {#346 ▼ 
       #items: array:1 [▼ 
        0 => Employee {#347 ▼ 
        #guarded: array:1 [▶] 
        #hidden: array:1 [▶] 
        #connection: null 
        #table: null 
        #primaryKey: "id" 
        #perPage: 15 
        +incrementing: true 
        +timestamps: true 
        #attributes: array:3 [▶] 
        #original: array:3 [▶] 
        #relations: [] 
        #visible: [] 
        #appends: [] 
        #fillable: [] 
        #dates: [] 
        #dateFormat: null 
        #casts: [] 
        #touches: [] 
        #observables: [] 
        #with: [] 
        #morphClass: null 
        +exists: true 
        +wasRecentlyCreated: false 
        } 
       ] 
       } 
      ] 
+1

** $ others **的類型是什麼,它是一個集合嗎?你的控制器中的「dd($ others)」可以告訴你。 –

+0

請寫下你的控制器函數返回這個視圖。 –

+0

@MustafaEhsan請檢查 –

回答

0

嘗試的first()代替get()這裏:

$emptyarray = Employee::where('employeeID', '=', $others->employeeID)->first(array('employeeID','email','fullName')); 

如果你仍然想使用get(),然後做如下:

foreach($other_on_leave as $others) { 
    if($emp_id != $others->employeeID) { 
     $emptyarray = Employee::where('employeeID', '=', $others->employeeID)->get(array('employeeID','email','fullName')); 

     foreach($emptyarray as $arr){ 
      $employees_on_leave[] = $arr; 
     } 
    } 
} 

而改變這一行也,所以視圖可以underst以及如何循環:

return view('admin.leave_applications.show', ['others' => $this->data['others', 'leave_application' => $this->data['leave_application']]); 

讓我知道它是否工作。

+0

我需要取得所有結果,將使用first()爲我做這個? –

+0

是不是**員工**表中唯一的** emplyeeId **? –

+0

是的,它是獨一無二的............... –