laravel blade @include not working

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-29 01:41:27

问题


I am trying to include files in my homepage using blade syntax :-

@foreach($data as $articles)
    @include(app_path().$articles->path)
    <br />
@endforeach

This is not working.

The error says :-

View [/var/www/blogproject/project/app/pages/articles/first-post.php] not found

I even tried including just the first page :-

@include(app_path().'/pages/articles/first-post.php')

But the normal php include is working fine :-

<?php include(app_path().'/pages/articles/first-post.php'); ?>

Please help


回答1:


That's because that file is not in the app/views directory. When you call @include('filename'), Blade automatically looks for any file with that name, inside the apps/views directory. Also, you're not supposed to write the file extension, as Blade automatically looks for files with .blade.php and .php extensions.

If you want to use files from other directories on the @include tag, add the directory to the paths array, on app/config/view.php. In your case, it'd be something like this:

app/config/view.php

<?php

    // ...

    'paths' => array(
        __DIR__.'/../views',
        __DIR__.'/../pages'
    );

Then, you'd call it on blade like so:

@include('articles/first-post')


来源:https://stackoverflow.com/questions/21474638/laravel-blade-include-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!