Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel blade @include not working

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

like image 722
Stacy J Avatar asked Jan 31 '14 07:01

Stacy J


People also ask

How to create view file in Laravel?

You may create a view by placing a file with the . blade. php extension in your application's resources/views directory.

How does Laravel Blade work?

Laravel Blade template engine enables the developer to produce HTML based sleek designs and themes. All views in Laravel are usually built in the blade template. Blade engine is fast in rendering views because it caches the view until they are modified. All the files in resources/views have the extension .

Why use Blade in Laravel?

In addition to template inheritance and displaying data, Blade also provides convenient shortcuts for common PHP control structures, such as conditional statements and loops. These shortcuts provide a very clean, terse way of working with PHP control structures while also remaining familiar to their PHP counterparts.

What is are the advantage of using Blade templates?

Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. All Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application.


1 Answers

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')
like image 62
rmobis Avatar answered Sep 29 '22 16:09

rmobis