Laravel Collection Get方法未按预期工作


Laravel Collection Get method not working as expected

我在php手工修补程序中尝试了以下操作:

  >>>  $contract = 'App'ContractHour::where('user_id', '=', 14)->get();
  =>  Illuminate'Database'Eloquent'Collection {#747
        all: [...]
      }
  >>>  $contract->where('day_of_wk', 1);
  => Illuminate'Database'Eloquent'Collection {#719
       all: [
         App'ContractHour {#726
           user_id: 14,
           day_of_wk: 1,
           start_time: "09:00:00",
           end_time: "05:00:00",
           break_time: "01:00:00",
           created_at: "2016-01-09 17:49:22",
           updated_at: "2016-01-09 17:49:22",
         },
       ],
     }
  >>> $contract->where('day_of_wk', 1)->get('start_time');
  => null

start_time明显存在时,为什么get方法返回null?

您错误地使用了get()方法。它的作用是返回一个集合,如果您只需要一个符合条件的项,那么请尝试使用first()。

$contract->where('day_of_wk', 1)->first()->start_time; //outputs 09:00:00