gpt4 book ai didi

php - Laravel 8 : Undefined variable when loading a Blade into another Blade

转载 作者:行者123 更新时间:2023-12-04 07:28:09 25 4
gpt4 key购买 nike

情况:
我正在制作一个侧面菜单,该菜单通过从表 (MySQL) 中获取数据来填充。
<li>代表菜单的一项:

<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
等等...
问题:
当我看到它的 Blade 时一切正常,但是当我将它包含在主 Blade 中时,它会抛出变量 $all_items包含制作 <li> 的数据无法识别 (Error: "variable not found error") 代码:
web.php
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('items', 'MenuCmpItemsController@show');
MenuCmpItemsController.php
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\MenuCmpItems;
use DB;

class MenuCmpItemsController extends Controller
{
public function show(){
$all_items = DB::table('tbClassrooms')->select("room_number")->get();
return view('items', ['all_items'=>$all_items]);
}
}
items.blade.php (访问 URL '/items' 时工作正常)
@foreach ($all_items as $item)
<li>{{ $item->cmp_name }}</li>
@endforeach
现在我想在 auth.blade.php 中包含 items.blade.php ,它是包含以下侧菜单结构的那个:
<div class="collapse show" id="sidemenu">
<ul class="nav">
@include('items')
</ul>
</div>
但它抛出

$all_items is undefined


我试过的:
  • 我检查了它是否可能是其他东西,例如,我用文本替换了“$all_items”,然后它在 auth.blade.php 中加载了文本,所以问题似乎是变量 $all_items
  • 我检查是否
  • return view('items')->compact('all_items');
    会有所作为,但没有。
    任何建议或想法都非常感谢。

    最佳答案

    你的文件夹结构是什么?在资源文件夹中

    关于php - Laravel 8 : Undefined variable when loading a Blade into another Blade,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68110107/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com