5

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

Stacy J
  • 2,721
  • 15
  • 58
  • 92

1 Answers1

21

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')
rmobis
  • 26,129
  • 8
  • 64
  • 65
  • after i add __DIR__.'/../pages' do i need to run any comman in the terminal - I did this but after refreshing it says View [articles/first-post.php] not found – Stacy J Jan 31 '14 at 09:22
  • ya, without .php its working in both cases - but y is tat, is this the right way to do it? – Stacy J Jan 31 '14 at 09:45
  • Well, Blade's `@include` is meant for `.blade.php` and `.php` files only. So they made it so you don't actually need to write the extension and then made a dot notation possible. Like, `articles/first-post' is exactly the same as 'articles.first-post'. So, yes, I guess not writing the extension is the right way to go. – rmobis Jan 31 '14 at 09:50
  • any idea how to put in the foreach loop, I am having troble ther also. @include($articles->path) does not seem to work – Stacy J Jan 31 '14 at 09:59
  • Try `@include(preg_replace('(path/|\.php)', '', $article->path))` – rmobis Jan 31 '14 at 15:05
  • 1
    @rmobis Can't thank you enough about the "Dont add the extension in the include". No wonder my @include('next.blade.php') wasn't working....I was intuitively adding the extension like in php includes... – powersource97 Dec 20 '16 at 08:40
  • Might be worth adding if you're on a new version of Laravel (5+) you need to write @include('articles.first-post') and not @include('articles/first-post') – Mark Twigg Jan 05 '17 at 13:53