我的foreach循环无法像我之前所做的那样工作

问题描述 投票:1回答:3

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

用户模型

protected $table = 'users';

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

UserController

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
html laravel foreach laravel-6
3个回答
1
投票

为什么使用数组?

    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>

0
投票

我看到您在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>

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.