Invalid argument supplied for foreach () with laravel

0

I have this error,

  

Invalid argument supplied for foreach () (View:   C: \ xampp \ htdocs \ bancohdvdao \ resources \ views \ aspiring \ create.blade.php)

$aspirante = new Aspirante;
$correo = DB::table('users')->where('id', 'Auth::user()->id')->value('email');
return view('aspirante.create', ["aspirante" => $aspirante])->with('correo', 
$correo); 

and the HTML

 <div class="col-md-3">
    <div class="form-group row">
       <label class="col-sm-12 col-form-label">Correo:</label>
          <div class="col-sm-12">
             @foreach($correo as $correo)
             <input type="text" id="correoasp" class="form-control" name="correo_asp" value="{{$correo->email}}" placeholder="" disabled="disabled">
             @endforeach
          </div>
     </div>
 </div>
    
asked by Santiago L Morales 30.11.2018 в 18:07
source

1 answer

0

The variable mail is not clear to me if you expect it to be an array or a string. As the flames declare, it should be a string since you are looking for mail that matches the user's id, and the id must be unique, then you should only get a single value. However, in the view you want to traverse it within the loop, which indicates that it should be an array or object.

If you want to obtain several emails you should not match in the method where the id to a unique one, but to those that you want to obtain. Here I do not know what condition you are looking for: all emails or ...?

If you just want to get the user's id, the foreach is left over.

Try also to remove the quotes from 'Auth :: user () - > id'

Regardless of whether this solves the error or not, it would be better to call the variable you run in the foreach in another way:

foreach($correo as $data)
...

I would do it like this:

$aspirante = new Aspirante;
$correo = Users::find(Auth::user()->id)->email;
return view('aspirante.create', compact('aspirante','correo');

and in the view:

<div class="col-md-3">
    <div class="form-group row">
       <label class="col-sm-12 col-form-label">Correo:</label>
          <div class="col-sm-12">
             <input type="text" id="correoasp" class="form-control" name="correo_asp" value="{{$correo}}" placeholder="" disabled="disabled">
          </div>
     </div>
 </div>
    
answered by 30.11.2018 в 19:14