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

1 Answer 1

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') 
Sign up to request clarification or add additional context in comments.

7 Comments

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
ya, without .php its working in both cases - but y is tat, is this the right way to do it?
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.
any idea how to put in the foreach loop, I am having troble ther also. @include($articles->path) does not seem to work
@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...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.