How to pass data from a controller to a module included with @include Laravel Blade?

1

I am developing a web with Laravel and I need to pass the data that comes from a controller to a module called seciones.blade.php that is in a folder called includes , so that the structure is like this:

includes/secciones.blade.php
index.blade.php

In the file index.blade.php I have included the file in question with Laravel Blade in the following way:

<!DOCTYPE html>
<html lang="es">
<head>
    <title>Innova Soluciones | Home </title>
</head>
<body>
    @include('includes/secciones')
</body>

I have the following path that takes me to a Controller named PrincipalController@index :

Route::get('/', 'PrincipalController@index');

PrincipalController@index executes the index method that what it does is to return the view where the includes/secciones.blade.php module is included in the following way:

class PrincipalController extends Controller
{
    public function index() {
        return view('index');
    }
}

Inside the file includes/secciones.blade.php I have a static menu that I want to replace it with the data that is the database:

<section class="seccion_categorias_items">
    <div class="item">
        <label class="item_img"><img src="img/logos/svg/zapatos.svg"></label>
        <h1 class="item_titulo">Innovate Zapatos </h1>
        <a class="item_link" href="/"></a>
    </div>
    <!-- ... las demas opciones aquí --> 
</section>

The process of passing the controller data to this module that is outside or in another location where the view index.blade.php is the one that I do not know, since the method index of PrincipalController would only return the% view % co with the ESA data view and not the index.blade.php module.

I hope you have explained to me, if not, I ask you to kindly ask, I appreciate your contributions or suggestions.

    
asked by Odannys De La Cruz 21.06.2018 в 00:08
source

1 answer

1

First of all you have to return that data in your controller

class PrincipalController extends Controller
{
    public function index() {
        $data = ['opciones'=>$opciones];
        return view('index')->with($data);;
    }
}

Then you must pass that data as a parameter in your @include

@include('includes.secciones', ['data.opciones' => 'opciones'])

And in your file includes/secciones.blade.php you should use that data

<h4>Mis opciones</h4>
@foreach ($opciones as $opcion)
    <p>{{ $opcion }}</p>
@endforeach

Everything explained is in the official documentation of Laravel about Blade Templates

    
answered by 21.06.2018 / 00:30
source