ErrorException 未定义变量 $teambatter(视图:C:\xampp\htdocs\ContentBaseApp - 1.0.2\resources\views\products\indexpublic.blade.php)

ErrorException Undefined variable $teambatter (View: C:\xampp\htdocs\ContentBaseApp - 1.0.2\resources\views\products\indexpublic.blade.php)

这里我有两个文件夹,分别命名为products 和teamname。在团队名称中,我有 index.blade.php,我可以在其中查看这样的名称 使用此代码

<td>{{ $teambatter->name }}</td>

哪里TeambatterController.php有这个代码

public function index()
   {
       $teambatter = Teambatter::latest()->paginate(5);

       return view('teambatters.index',compact('teambatter'))
           ->with('i', (request()->input('page', 1) - 1) * 5);
   }

问题是当我使用此代码在产品文件夹中的 indexpublic.blade.php 中查看相同名称时

<h1> {{ $teambatter->name }} </h1>

我在 indexpublic.blade.php 中得到 $teambater 未定义。 如何将此名称添加到产品文件夹中的 indexpublic.blade.php?

它不起作用如果我在我的 productConteroller.php

中这样通过
 public function indexpublic()
    {
        $teambatter = Teambatter::latest()->paginate(20);
         $products = Product::latest()->paginate(20);
         return view('products.indexpublic',compact('products'))
         ->with('i', (request()->input('page', 1) - 1) * 5);

    }

这是我的web.php

Route::get('/', [ProductController::class, 'indexpublic']);

在 indexpublic() 函数中,您将 $products 变量传递给视图,而不是 $teambatter 变量。

这应该有效:

public function indexpublic()
{
  $teambatter = Teambatter::latest()->paginate(20);
  $products = Product::latest()->paginate(20);
  return view('products.indexpublic',compact('products', 'teambatter'))->with('i', (request()->input('page', 1) - 1) * 5);
}