TheeShot's avatar

Creating an index overview

Hi all,

I am VERY new to Laravel, but been working my way through some laracast tutorials and I am really liking it.

Working on a small project with laravel, I created an admin area. My views are stored in an "admin" folder like so:

  • admin
    • stages (folder)
      • create.blade.php
      • edit.blade.php
      • index.blade.php
      • show.blade.php
    • teams (folder)
      • create.blade.php
      • edit.blade.php
      • index.blade.php
      • show.blade.php
    • index.blade.php

I got it all to work, but now want to use the index.blade.php in the admin root to show the same content of the index files in the folders but then smaller using bootstrap. Like stages 8 rows and teams 4 rows. That way I am creating a nice interactive overview.

I tried with includes, but the problem is my controller directs variable inside the folders, giving me an error on the root index page.

ErrorException Undefined variable: stages (View: E:\laravel\Learnproject\resources\views\layouts\admin-stages.blade.php) (View: E:\laravel\Learnproject\resources\views\layouts\admin-stages.blade.php)

Stages/index.blade.php

@extends ('layouts.admin-master')

@section ('content')
    
        @include('layouts.admin-stages')

@endsection

Admin/index.blade.php

@extends ('layouts.admin-master')

@section('content')

    @include('layouts.admin-stages')

@endsection

Routes example:

// Stages summary
Route::get('admin/stages', 'StagesController@index');

//add stages
Route::get('admin/stages/create', 'StagesController@create');
Route::post('admin/stages', 'StagesController@store');

// details stages
Route::get('admin/stages/{stage}', 'StagesController@show');

//edit stages
Route::get('admin/stages/edit/{stage}', 'StagesController@edit');
Route::patch('admin/stages/update/{stage}', ['as' => 'stage.update', 'uses' => 'StagesController@update']);

//remove stages
Route::get('admin/stages/delete/{stage}', ['as' => 'stage.delete', 'uses' => 'StagesController@delete']);

I hope I could clearify my issue and that someone can help.

Kind regards TShot

0 likes
2 replies
Snapey's avatar

You need to run the same queries as in the other index controllers and pass it to a new view file. This can call in partials if you extract the re-usable parts from the other views.

Jaytee's avatar

Take a look at view composers in the documentation.

View Composers allow you to share data across a set of views (that you specify) so you don't need to keep passing them from the controller.

Here is the link, and here is a demo for your project: https://laravel.com/docs/master/views#view-composers

// in your AppServiceProvider in your Boot method (or create your own provider)

View::composer('layouts.admin-stages', function ($view) {
    $myVariable = 'Hello World';
    $view->with('myVariable', $myVariable);         
});

The first argument to the composer method is the view name, so if you wanted to share $myVariable to a view called admin.pages.index, you'd pass that in.

The second argument accepts the view, Laravel will do this automatically, you just need to accept the $view variable.

Then you can pass anything you need to it, as you normally would.

Please or to participate in this conversation.