【问题标题】:Undefined variable: categories in blade laravel 5.6未定义变量:刀片 laravel 5.6 中的类别
【发布时间】:2019-01-19 15:42:29
【问题描述】:

我正在尝试在站点地图网页中显示我的类别 这是我的CategoryController.php

namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{

public function site()
   {
    $categories = Category::all();
    #   return view('template.sitemap', compact('categories'));
    #   return view('template.sitemap',['categories' => $categories]);
     return view('template.sitemap')->with(compact('categories'));
    #   return view('template.sitemap')->withData($categories);
    #   return view('template.sitemap')->with('categories',$categories);
    #   return view('template.sitemap', compact('categories'));
  }
}

我使用的 # 显示我使用不同的方法传递变量来查看 我单独使用每种方法,但没有一种方法不起作用。

这是我在模板文件夹中的站点地图刀片文件的一部分,我使用类别变量template.sitempa.blade.php

        <div id="content" class="col-sm-12">
                <h1 class="title"> site map</h1>
                <div class="row">
                    <div class="col-sm-3 hidden-xs hidden-sm sitemap-icon"><i class="fa fa-sitemap"></i></div>
                    <div class="col-md-5 col-sm-6">
                        <ul class="sitemap">
                                <ul>
                                    @foreach($categories as $category)
                                        <li><a href="category.html">{{$category['name']}}</a></li>
                                    @endforeach

                                </ul>
                        </ul>
                    </div>
                </div>
            </div>

我使用各种获取变量和传递的方式,但没有一个不起作用 你有什么建议吗?

【问题讨论】:

  • 我认为template.sitempa.blade.php有输入错误,应该是template.sitemap.blade.php

标签: php laravel controller laravel-blade php-7.1


【解决方案1】:

你的刀片文件应该在目录template/sitemap.blade.php,而不是template.sitempa.blade.php

public function site()
{
    $categories = Category::all();
    return view('template.sitemap', compact('categories'));
}

@foreach($categories as $category)
    <li><a href="category.html">{{ $category->name }}</a></li>
@endforeach

【讨论】:

  • 再次出现同样的错误,我在项目的任何地方都找不到任何问题
  • 什么错误显示,返回前dd($categories),类别表中没有数据?刀片应该在 /view/template/sitemap.blade.php 中,由 template.sitemap 使用
  • 不要像 example.something.etc.blade.php 这样命名你的刀片文件,而是像这样 example_something_etc.blade.php 进行更改
  • 我写了 dd($categories) 但我不知道在哪里寻找出口?我应该在哪里看到 dd 的导出?
  • 把 dd($categories) 放在$categories = Category::all(); 屏幕上显示的任何东西下面?
【解决方案2】:

试试这个:

public function site()
{
    return view('template.sitemap')->withCategories(Category::all());
}

那么在你看来,做:

dd($categories);

了解您是否正在接收数据。如果没有问题,请根据需要使用它:

@foreach($categories as $category)
    <li><a href="category.html">{{$category['name']}}</a></li>
@endforeach

【讨论】:

  • 再次未定义变量
【解决方案3】:

试试这个

public function site()
{
$this->data['categories'] = Category::all();
return view('template.sitemap',$this->data);
}

在视图中

@foreach($categories as $category)
<li><a href="category.html">{{$category->name}}</a></li>
@endforeach

【讨论】:

    猜你喜欢
    • 2017-06-17
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 2014-03-15
    • 2017-08-24
    • 2022-11-02
    • 1970-01-01
    • 2019-02-19
    相关资源
    最近更新 更多