skip to Main Content

I want a single row of player through his playerNo field which is later added and updated.

I am doing this.

$player = Account::where('playerNo', 99)->get();

Result I am getting is many objects of players that only has player object with playerNo, which I do not want because for that I have to use iteration.

foreach($players as $player)
   $player->name = "Sadan"

2

Answers


  1. To get only a single Model returned use first().

    eg

    $player = Account::where('playerNo', 99)->first();
    

    However if you were getting multiple models by using get() then you have multiple models that match with a playerNo of 99 which seems odd.

    Login or Signup to reply.
  2. To get single row in Laravel you can use first() in model or controller below;

    $player = Account::where(‘playerNo’, 99)->first();

    To get data in blade below::

    Name : {{ $player->name }}

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search