簡體   English   中英

如何在Laravel 5中訪問集合的單一值屬性

[英]How to access single value property of a collection in laravel 5

我的控制器中有一個通過控制器傳遞的變量:name是$ others

當我做一個foreach並打印時

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

輸出為:

[{"employeeID":"9125123981003","email":"admin@gaincafe.com","fullName":"Pranshu Jain"}]     [{"employeeID":"45755757577","email":"yoyo@pranshu.com","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($ others)然后我得到

                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
                  }
                ]
              }
            ]

在這里嘗試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;
        }
    }
}

並更改此行,以便視圖可以理解要遍歷的內容:

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

讓我知道它是否有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM