【问题标题】:view()->share() is not working in laravelview()->share() 在 laravel 中不起作用
【发布时间】:2020-05-06 10:25:13
【问题描述】:

我有这个错误

未定义变量:theloai(查看:C:\xampp\htdocs\LaravelDemo\resources\views\layout\menu.blade.php)

这是我的控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\TheLoai;

class PagesController extends Controller
{
    //
    function __contruct()
    {
        $theloai = TheLoai::all();
        view()->share('theloai', $theloai);
    }

    function trangchu()
    {
        return view('pages.trangchu');
    }

    function lienhe()
    {
        return view('pages.lienhe');
    }
}  

这是我的菜单我有这个错误

未定义变量:theloai(查看:C:\xampp\htdocs\LaravelDemo\resources\views\layout\menu.blade.php)

<div class="col-md-3 ">
    <ul class="list-group" id="menu">
        <li href="#" class="list-group-item menu1 active">
            Menu
        </li>

        @foreach($theloai as $tl)
            @if(count($tl->loaitin) > 0)
                <li href="#" class="list-group-item menu1">
                    {{ $tl->Ten }}
                </li>

                <ul>
                    @foreach($tl->loaitin as $lt)
                    <li class="list-group-item">
                        <a href="#">{{ $lt->Ten }}</a>
                    </li>
                    @endforeach
                </ul>
            @endif
        @endforeach
    </ul>
</div>

【问题讨论】:

  • 你试过使用 Facede 吗? View::share('key', 'value'); ?
  • function __contruct() 可以解决这个错字吗?
  • 你可能想看看view composers
  • 我试过 View::share('key', 'value'); 不能工作
  • 更正构造函数中的错字,它是__construct()。再试一次

标签: laravel view


【解决方案1】:

尝试使用 wrap() 代替 share() 请在此处参考新方法 https://laravel.com/api/5.4/Illuminate/Container/Container.html

【讨论】:

  • 打不开,提示找不到
【解决方案2】:

我试着把 $theloai = TheLoai::all(); 视图()->分享('theloai',$theloai);到 App/Providers/AppServiceProvider 和它的工作人员

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-11
    相关资源
    最近更新 更多