PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Blocks

    Design blocks for your website

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Question By
Unsolved

Include .php file in Blade.php

meaghanpsavage

Jan 23rd, 2023 01:35 PM

I've added a function in a new php file under the views > themes > dashboard folder that I am trying to include in the index.blade.php file under the same dashboard folder to reference it.

I'm trying to include with standard php syntax below:

However, this is breaking my entire dashboard view. I'm not sure what the issue could be here. Any recommendations for what to do?

thinkverse

Jan 23rd, 2023 01:43 PM

Hard to debug without seeing the code or error if one is produced. You can add code blocks by using three backticks.

```
// Your code here.
```

Or using Paste, or GitHub Gist, Gist required a GitHub account though, but great if you need to show multiple files. And then you can link it by using the following markdown format.

[Link description](URL)
meaghanpsavage

Jan 23rd, 2023 01:55 PM

Didn't realize it deleted when I posted, I am using standard php file syntax for including other php files that I am familiar with.

I added the below code directly into the index.blade.php file within the dashboard view directory. I added my new php file that I am looking to call into the same directory.

// <?php
//      include "filename.php"
// ?>

Also, it is a 500 Server Error that I receive when reloading the page.

thinkverse

Jan 23rd, 2023 02:11 PM

Are you wanting to use it as a component, if that's the case Laravel Blade has built-in support for components.

If you just want to include it, then you'll have issues with finding the right path, since Laravel caches views. Are you including it because of a single function? Depending on what that function does you have options.

You could extend Blade and add your own directive that handles what your function did.

Or you can made the functions available globally with the help of Composer and autoload the file. You do that by adding the path to the file in an array under a files key in your composer.json

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    },
    "files": [
        "path/to/function/file.php"
    ]
},

Then you need to update the autoloader by dumping it.

composer dump-autoload

With that you should be able to use any of your functions in Blade.

{{ function('arguments') }}
thinkverse

Jan 23rd, 2023 02:21 PM

As for the 500 internal error, you can find the application logs under storage/logs, there should be a laravel.log file in there, it appends to the file so your latest logs entry will be at the bottom of the file.

meaghanpsavage

Jan 23rd, 2023 02:30 PM

Thank you, this was very helpful. I am looking for several functions within to be global functions so the composer autoload option works well for my use case.

I ran this and was able to reload my website and execute the code from my other function.

I did include the full file path in the composer.json. Do you know if I can shorten this down at all based on where it is located in the wave folder?

    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/",
            "Wave\\": "wave/src/"
        },
        "files": [
            "C:/wamp64/www/wave/resources/views/themes/tailwind/dashboard/file.php"
        ]
    },
thinkverse

Jan 23rd, 2023 02:38 PM

It will be relative to the project root I believe, or the composer.json which usually is in the project root. So in this case the path would be:

"files": [
    "wave/resources/views/themes/tailwind/dashboard/file.php"
]

Though I'd recommend keeping it out of the themes folder and only keeping your views and Blade components in there. You might've noticed the path includes resources/views.

I personally place my helper file in app/helpers.php, that's sort of the "standard" for Laravel.