Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

395
Vistas
How to return multiple views in one function in laravel for same route

it is quite difficult to explain my question. I hope you will be able understand it.

For my project I have a master layout which contains the header and yield of content.

master.blade.php

 @include('partials.header')
    <div class="container-fluid main-con">
        @yield('content')
        @include('partials.footer')
    </div>  

now on main public route I am raturning a method.

Web.php

Route::get('/', [
    'uses' => 'ProductContoller@getIndex',
    'as' => 'product.mainCats'
]);

ProductContoller.php

class ProductContoller extends Controller
{

    public function getIndex(){
        $ad_cats = Mainaddtype::orderBy('title')->get();
        return view( 'shop.main-categories-page', ['mediacats' => $ad_cats]);
    }
}

Now my query is, 'main-categories-page' is yielding in content section of master. but I want the same data of Mainadtype in header also. So I want to return view of header on same function. Please help if u understand it.

what I am trying now is,

 public function getIndex(){
        $ad_cats = Mainaddtype::orderBy('title')->get();
        return view( 'shop.main-categories-page', ['mediacats' => $ad_cats]);
        return view( 'partials.header', ['mediacats' => $ad_cats]);
    }

But I know we cant return in this way. Thanks in advance.

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You can access the variable in the header as well. Just have a check to see if the variable exists so that you don't get errors when you load other views.

@if(!empty($mediacats))
   // do awesome stuffs with $mediacats
@endif

This way your data will be available when you are on index page.

If you want the data to be available in all views, you can use laravel view composer. From official docs:

View composers are callbacks or class methods that are called when a view is rendered. If you have data that you want to be bound to a view each time that view is rendered, a view composer can help you organize that logic into a single location

Hope it helps.

about 4 years ago · Santiago Trujillo Denunciar

0

function gets terminate as it gets the first return statement and also two views can't be returned in laravel, the only possible thing you can do is include view in layout template. example adding category view in template.

@if(showStore)
@include('store.category')
@endif
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda