Select a record of an array in Laravel 5.3

0

Hi, I want to select a data that is stored in an array, for example the data I need is in position 1, but I do not know how to select it:

My controller is the following, I have minimized it for greater understanding:

public function listaAlumnos()
    {
        $alumnos = DB::table('alumno')
        ->select('idalumno')
        ->get();

        $pr = $alumnos->idalumno[1]; //Necesito que el registro en la posición 1 se guarde en $pr.
    }

If someone could help me, thank you in advance.

    
asked by Anddy Cabrera Garcia 12.02.2017 в 07:13
source

1 answer

3

When using get () you are recovering an array of students, so you should use a foreach ():

$alumnos = DB::table('alumno')->select('idalumno')->get();
foreach($alumnos as $alumno){
  $pr = $alumnos->idalumno;
}

If you expect to recover only one student you can use first ():

$alumnos = DB::table('alumno')->select('idalumno')->first();
$pr = $alumnos->idalumno;
    
answered by 14.02.2017 в 10:09