如何使用laravel使用组件显示动态导航栏? [英] How do I use components to display my dynamic navigation bar using laravel?

查看:64
本文介绍了如何使用laravel使用组件显示动态导航栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,当我显示页面内容时,我无法显示导航栏.目前,我的代码输出是未定义的变量:navContent(视图:C:\ Users \ Computer Angel \ Documents \ blog \ resources \ views \ layouts \ components \ navbar.blade.php).我想要的输出是显示导航栏,然后显示其下方的页面内容.

I have an issue where I can't display my navigation bar when I am displaying my page content. Currently the output of my code is, Undefined variable: navContent (View: C:\Users\Computer Angel\Documents\blog\resources\views\layouts\components\navbar.blade.php). My desired output is to show my navigation bar, then my page content below it.

这是我的dynamic.blade.php代码,在该代码中应同时显示导航栏和页面内容.

This is my code for the dynamic.blade.php which is where both the navigation bar and the page content should be displayed.

<!DOCTYPE html>
<html>
    <head>
    </head>
    @component('layouts.components.navbar')
    @endcomponent
    <body>
        {!!$pageContent->pageContent!!}
    </body>
</html>

也是我的组件的navbar.blade.php显示如下:

The navbar.blade.php which is also my component is shown below:

<nav>
    @slot('$navContent')
    @foreach($navContent as $nav)
    <a href="{!!$nav->navLink!!}">{!!nav-navName!!}</a>
    @endforeach
    @endslot        
</nav>

这是我的PageController @ show:

This is my PageController@show:

public function show($URI)
    {
        $pageContent = DB::table('pages')->where('URI',$URI)->first();
        return view('page.dynamic', ['pageContent' => $pageContent]);
    }

这是我的Nav.php

This is my Nav.php

protected $fillable = ['navName', 'navLink'];

这是我的Page.php

This is my Page.php

protected $fillable = ['title', 'URI', 'pageContent'];

这是我的页面迁移:

Schema::create('pages', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('URI');
            $table->text('pageContent');
            $table->timestamps();
        });

这是我的导航迁移:

Schema::create('navs', function (Blueprint $table) {
            $table->id();
            $table->string('navName');
            $table->string('navLink');
            $table->timestamps();
        });

这是我的web.php:

This is my web.php:

Route::get('/page/{URI}', 'PageController@show');

有关我的完整代码,请使用下面的GitHub Repository链接:

For my full code, use the GitHub Repository link below:

https://github.com/xiaoheixi/blog

感谢您抽出宝贵的时间阅读并尝试帮助我!:D

Thanks for taking the time to read and try to help me! :D

推荐答案

我只是按照下面列出的当前步骤解决了问题.

I just fixed my problem by doing my current steps listed below.

  1. 我看过 https://www.youtube.com/watch?v=THN5Q8JBeDM.
  2. 添加了" $ navContent = Nav :: all();view()-> share('navContent',$ navContent);"boot()公共函数中访问AppServiceProvider.php.
  3. 添加使用App \ Nav;进入AppServiceProvider.php.
  4. 删除navbar.blade.php中的@slot('$ navContent')和@endslot.
  5. 打开终端并运行php artisan serve,它应该可以工作.

最后的祝愿,希望大家都通过您的专业经验并毕业.

Finally best wishes, hope you all pass your Professional Experience and graduate.

这篇关于如何使用laravel使用组件显示动态导航栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆