I got this error in one of the drivers in laravel. "Can not use scalar value as an array", how should I solve it?

-2
 $formacademica = new Formacademica;
 $userCorreo = Auth::user()->email;  

 $userCorreo = Auth::user()->email; /*Aqui solo capturo el usuario que ha iniciado sesión*/

 $aspirantes[] = DB::table('aspirantes')->where('correo_asp',$userCorreo)->first(); 
 /*en esta parte lo que hago es poner una condición con where, donde comparo el campo 
 correo_asp con la variable $userCorreo, para obtener los datos que tiene ese correo. Y es donde tengo el error*/
    
asked by Santiago L Morales 04.12.2018 в 22:56
source

3 answers

2

Dear, this type of PHP error indicates that you are using a variable with a scalar value ( string , float or integer ) as an array. When you try to do this, PHP sends a warning, this you can check with the function is_scalar of PHP that lets you know if a variable is scalar.

According to the code you offer, I think that when you use $aspirantes[] is when you throw this exception, you must first check that this variable is initialized as a array or verify that it was not previously created as a scalar variable.

    
answered by 05.12.2018 в 15:29
0

Try it this way:

$userCorreo = Auth::user()->email;  

$userCorreo = Auth::user()->email;

$aspirantes = DB::table('aspirantes')->where('correo_asp',$userCorreo)->get(); 

in the view you go through it like this:

@foreach($aspirantes as $asp)
  {{$asp->correo_asp}}
@endforeach
    
answered by 05.12.2018 в 19:29
-1

Why the brackets?

It should just be like this

 $aspirantes = DB::table('aspirantes')->where('correo_asp',$userCorreo)->first(); 

The query you are doing only returns an object, not an array, it is not necessary to use a foreach

Now in the view you should place something like the following

<div class="col-md-3">
    <div class="form-group row">           
          <div class="col-sm-12">
            @if($aspirantes)
                {{ $aspirantes->id_asp }}
            @endif
          </div>
     </div>
 </div>

I think they answered the same thing in another question you did.

Invalid argument supplied for foreach () with laravel

    
answered by 04.12.2018 в 23:13