Ad

Laravel 5.2 Carousel Only In Index Page Doesn't Wok

I use laravel 5.2 and tried to display a carousel only in index page but doesn't work. I chose that the codes are not "spreaded" on the index page, they are stored in: public/carousel/carousel.php, too(not ...blade.php).

route.php:

Route::get('/', '[email protected]')

HomeController.php:

    {
        $cats = Category::all();
        $carousel = public_path('carousel/carousel.php');
        //$carousel = storage_path('public/carousel/carousel.php');
        return view('layouts.app', compact('cats', 'carousel'));
    }

layouts/app.blade.php:

 {{-- @include('carousel/carousel');--}}

            @if($carousel)
            {{ $carousel }}
            @endif

            @yield('content')

Finally it displays only: C:\wamp\www\app_name\public\carousel/carousel.php. Can you help me or point to another better way?

Ad

Answer

In your controller, you are passing to the view a variable called $carousel, which is the path to your file, as you defined here:

$carousel = public_path('carousel/carousel.php');

This is the reason why it only displays the string. You need to get the actual content of the file:

$carousel = file_get_content(public_path('carousel/carousel.php'));

A better and more laravel-ish way to do it would be to rename the file to carousel.blade.php, store it into the resources/views folder and simply include it from your main blade file (without the need of doing anything in the controller):

@include('carousel')

If you need to display the carousel on certain pages only, you can simply pass a variable $carousel = true on the pages that needs to display it:

$carousel = true;
return view('layouts.app',compact('carousel'));

And in your blade view, include the carousel file only when this variables is present and is true:

@includeWhen(isset($carousel) && $carousel, 'carousel')
Ad
source: stackoverflow.com
Ad