Laravel: Only retrieve a specific property from the first matching model
Leonel Elimpe
by Leonel Elimpe
~1 min read

Tags

  • Laravel

Using Laravel Eloquent, I wanted to only get a specific property - the id - from the first matching model of my query.

Here’s how I did it:

<?php

use App\Promotion;

$promotionId = Promotion::where('code', 'GOOD_DEAL')->pluck('id')->first();

echo $promotionId // 1


I tried out a good number of eloquent method combinations before arriving at pluck() + first().

nly retrieve a specific property from the first matching model of a query - try outs

With respect to the above screenshot, GOOD_DEAL_500 is a valid promotion code and GOOD_DEAL_50 is not.

Of course, I could still just use Promotion::where('code', 'GOOD_DEAL')->first()->id, but this prints out:

PHP Notice:  Trying to get property 'id' of non-object in Psy Shell code on line 1

if no promotion is found, I wanted to avoid this 🤷🏻‍♂️.