Home > Software design >  "A four digit year could not be found" happens when a model tries to SELECT in Laravel
"A four digit year could not be found" happens when a model tries to SELECT in Laravel

Time:10-28

I would like to fetch names form this table. enter image description here

I tried these

Category::where('id','20')->value('name');

or

Category::find(20)->get("name")

however, error "A four digit year could not be found" happened.

CodePudding user response:

$cat = Category::query()->where('id','=',20)->first();
$name = null;
if(!is_null($cat)){
   $name = $cat->name;
}

should work

however if u use php 8 code will be shorter

$name = $cat?->name;

CodePudding user response:

Category::find($id, ['name']);

  • Related