adam78
adam78

Reputation: 10068

Laravel Collection Get method not working as expected

I've tried the following in php artisan tinker:

  >>>  $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

Why is the get method returning null when clearly the start_time exists?

Upvotes: 2

Views: 1670

Answers (1)

Philip I
Philip I

Reputation: 294

You are using the get() method incorrectly. What it does is returns a collection, if you need just one item matching your criteria, then try using first() instead.

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

Upvotes: 2

Related Questions