如何在laravel 5中访问集合的单值属性

时间:2015-10-07 09:12:57

标签: php laravel

我在视图中通过我的控制器传递了一个变量:  名称是 $ 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
                  }
                ]
              }
            ]

1 个答案:

答案 0 :(得分: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;
        }
    }
}

并且也改变了这一行,因此视图可以理解循环的内容:

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

让我知道它是否有效。