我的 foreach 循环没有像以前那样工作

My foreach loop is not working as the previous I've done

我是新手 Laravel(也是 Whosebug),我正在尝试使用 foreach 循环在 PhpMyAdmin 的 home.blade.php table 中显示数据。但是,它无法正常工作,我无法弄清楚问题出在哪里。我还有其他 table 与 foreach 一起工作,并且我对这个 table 执行了相同的步骤。

用户模型

protected $table = 'users';

protected $fillable = ['id','name','edad','direccion_personal','celular','foto','email','direccion_sucursal_id'];

用户控制器

public function index()
{
    $Usuarios = User::all();
    $array = ['usuarios' => $Usuarios];

    return view('home')->with($array);
}

最后,这是我的身体:

<tbody>
@foreach ($usuarios as $Usuarios)
    <div>
        <tr>
            <th scope="row" style="text-align:center;">{{ $Usuarios->id }}</th>
            <td style="text-align:center;">{{ $Usuarios->nombre }}</td>
            .
            .
            .
        </tr>
    </div>
</tbody>
@endforeach

我发现您在使用 foreach 循环时遇到了问题。无法正常工作...但是,我不确定是什么问题...如果我的回答不适合您,请更新您的问题以便获得更多帮助

我看到您关闭了 foreach 循环内部。 这样一来,您将以许多结束标签结束,只有一个开始...

尝试将关闭标记移到循环外

<tbody>
@foreach ($usuarios as $Usuarios)
 <div>
  <tr>
    <th scope="row" style="text-align:center;">{{$Usuarios->id}}</th>
    <td style="text-align:center;">{{$Usuarios->nombre}}</td>
       .
       .
       .
  </tr>
 </div>
@endforeach
</tbody>

为什么是数组?

    public function index(){
        $usuarios = User::all();
        return view('home', compact('usuarios'));
    }

然后:

<tbody>

@foreach ($usuarios as $us)
 <div>
  <tr>
    <th scope="row" style="text-align:center;">{{$us->id}}</th>
    <td style="text-align:center;">{{$us->nombre}}</td>
       .
       .
       .
  </tr>
 </div>
@endforeach
</tbody>

您的 foreach 在 </tbody> 标记外关闭,并在其中打开。因此,您的 table 主体在循环的第一次迭代后关闭,并且再也不会打开,因此每次迭代您现在都有一个额外的 </tbody> 行。这是无效的标记,会破坏您网站的输出。