0
votes

In the Laravel framework, I was wondering if it is possible to include a php file into the blade template, I have tried and failed and was wondering if there is another way I do not know of.

4
The obvious question here is why would you want to do that? I have a feeling there are better ways to do whatever it is you wish to do. - Andrei
Please give us a code example or explain what you are trying to do, there could be better/other solutions to your problem. - Thomas De Marez
Unsure of what reason you would have to do this if I am reading this correctly. If you're after using functions you may have created in another file they can be added to your helpers. Otherwise you could create another blade template file and include it how Yudi replied with. - Mikey

4 Answers

3
votes

Solution :

@include('admin.layouts.partials.styles')

it means :

File path will be

 admin > layouts > partials >styles (styles.blade.php is file)
1
votes

Try using include function at laravel blade template :

@include( <view path> )

example :

// File : resource/view/backend/page/form.php
@include( 'backend/page/form' )
1
votes

You can use the blade template engine:

@include('view.name') 

'view.name' would live in your main views folder

// for laravel 4.X app/views/view/name.blade.php

// for laravel 5.X resources/views/view/name.blade.php Another example

@include('hello.world');

would display the following view

// for laravel 4.X app/views/hello/world.blade.php

// for laravel 5.X resources/views/hello/world.blade.php Another example

@include('some.directory.structure.foo');

would display the following view

// for Laravel 4.X app/views/some/directory/structure/foo.blade.php

// for Laravel 5.X resources/views/some/directory/structure/foo.blade.php So basically the dot notation defines the directory hierarchy that your view is in, followed by the view name, relative to app/views folder for laravel 4.x or your resources/views folder in laravel 5.x

ADDITIONAL

If you want to pass parameters: @include('view.name', array('paramName' => 'value'))

You can then use the value in your views like so <p>{{$paramName}}</p>

1
votes

You can right raw php in blade using @php and the you can require the php file using require('somefile.php'); so the code will be as follows

@php
require('somefile.php');
@endphp

How ever this not recommended! Hope this helps!