Problem with blade directive and view rendering

bl8m posted 1 year ago

I'm trying to create a reusable view component for a carousel with a new Blade directive. I don't want to use an @include directive because in this case I have to load the carousel data in the controller, and otherwise I don't want to put the logic in the view.

The code for the directive in AppServiceProvider.php is this:

Blade::directive('carousel', function ($expression) {

            $carousel = Carousel::with('slide')->where('slug', '=', trim($expression, "'"))->first();

            $carousel_html = View::make('www.common._partials.carousel')
                        ->with('carousel', $carousel)

            //dd($carousel_html); // if I uncomment this, the dump is correct

            return $carousel_html;

in the page view, I used the directive this way:


Now, when I try to load the page, I get this error:

ErrorException in PhpEngine.php line 42:
include(/var/www/clients/client0/web30/web/storage/framework/views/3f66a91d2dbec576e8a5d0b20f2f9c0bb08d0a58.php): failed to open stream: No such file or directory

Probably is a cache issue, or maybe my approach is wrong and I don't have to use a directive. Any hint or suggestion? Thank you!

bl8m replied 1 year ago Solution

I'm not proud of this solution, but it works:

Blade::directive('carousel', function ($expression) {

           $slug = trim($expression, "'");
           return "<?php \$carousel = Carousel::with('slide')->where('slug', '=', '{$slug}')->first(); echo \$__env->make( 'www.common._partials.carousel' , ['carousel' => \$carousel])->render(); ?>";

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us