“未定义偏移:1”错误。这是怎么回事?

问题描述 投票:0回答:2

我试图用 foreach 传递数据库中的所有帖子,但会弹出此错误。

这是我的观点:

<ul>
  {{ <li>@foreach ($posts => $posts) }}
    <div class="row section scrollspy" style="padding:10px; margin-top:10px;">
      <div class="content-box col s12" style="padding:20px; margin-top:40px; background: #f2f2f2; border-radius:10px;">
        <div class="i_title" style="text-align:center; margin: -57px 0 0 0;">
          <a href="/profile" class="tooltip"><img class="responsive-img circle" src="images/profile_picture.jpg" style="width:60px; box-shadow: 0 6px 10px 0 rgba(0, 0, 0, 0.2);"><span class="tooltiptext">@Username{{ $user->username }}</span></a>
        </div>
          <p class="col s12"{{ $post->post }}</p>
      </div>
    </div>
  {{ @endforeach</li> }}
</ul>

我的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
  public function getUsers()
  {
    $user = DB::table('users')->get();
    return view('layouts/welcomeView', ['users' => $user]);
  }
  public function getPosts()
  {
    $posts = DB::table('posts')->get();
    return view('layouts/welcomeView', ['posts' => $posts]);
  }
}

我的数据库有一些帖子,所以这不是问题。我认为问题在于我没有很好地通过帖子传递变量。我能做什么??

php laravel laravel-blade
2个回答
1
投票

这里有一些问题。

首先,您正在混合刀片指令。

{{ }}
用于回显某些内容。
{{
}}
中的任何内容都被解释为 PHP,并被传递到函数中进行一些转义,然后被回显。因此,当您执行
{{ <li>@foreach ($posts => $posts) }}
时,Blade 编译器会尝试将
<li>@foreach ($posts => $posts)
视为 PHP 代码,这将导致语法错误。
@foreach
不属于
{{ }}
内部。

其次,我注意到你的

@foreach
里面有
$posts => $posts
。注意到箭头两边的变量是相同的吗?这无疑是一个拼写错误,但它会导致循环的第一次迭代用第一个条目覆盖
$posts
变量,并且循环将尝试继续迭代这个新的单个值。这很可能是导致您看到的错误的原因。

这可能更接近您正在寻找的内容:

<ul>
  @foreach ($posts => $post)
    <li>
      <div class="row section scrollspy" style="padding:10px; margin-top:10px;">
        <div class="content-box col s12" style="padding:20px; margin-top:40px; background: #f2f2f2; border-radius:10px;">
          <div class="i_title" style="text-align:center; margin: -57px 0 0 0;">
            <a href="/profile" class="tooltip"><img class="responsive-img circle" src="images/profile_picture.jpg" style="width:60px; box-shadow: 0 6px 10px 0 rgba(0, 0, 0, 0.2);"><span class="tooltiptext">@Username{{ $user->username }}</span></a>
          </div>
            <p class="col s12"{{ $post->post }}</p>
        </div>
      </div>
    </li>
  @endforeach
</ul>

0
投票

改变你的观点;

<ul>
   <li> 
   @foreach ($posts => $post)
    <div class="row section scrollspy" style="padding:10px; margin-top:10px;">
      <div class="content-box col s12" style="padding:20px; margin-top:40px; background: #f2f2f2; border-radius:10px;">
        <div class="i_title" style="text-align:center; margin: -57px 0 0 0;">
          <a href="/profile" class="tooltip"><img class="responsive-img circle" src="images/profile_picture.jpg" style="width:60px; box-shadow: 0 6px 10px 0 rgba(0, 0, 0, 0.2);"><span class="tooltiptext">@Username{{ $user->username }}</span></a>
        </div>
          <p class="col s12"{{ $post->post }}</p>
      </div>
    </div>
  @endforeach
  </li>
</ul>

如果未在某处定义,您可能需要移动

@Username

在 foreach 之前检查数组中是否有某些内容通常是一个很好的做法。

您可以通过以下方式实现这一目标:

@if(! $posts->isEmpty()) 
    @foreach ($posts => $post)
        ....
    @endforeach
@endif
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.