Search code examples
phplaravellaravel-5

Laravel - Load common header and footer to view


I am new to Laravel and I am trying to load header, footer and the view file from controller in a common template and display the data from controller in the view file. But I get error

View ['admin.dashboard'] not found.

The dashboard file is present in the admin folder inside views.

Controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class common extends Controller
{

   public function login()
   {
        $data['title'] = 'Dashboard';
        $data['template'] = 'admin/dashboard';
        return view('common_template', compact('data'));

   }
}

common_template.blade View

<?php echo View::make('includes/header'); ?>

<?php echo $template = "'".$data['template']."'";
echo View::make($template); ?>
<?php echo View::make('includes/footer'); ?>

When I add 'admin/dashboard' instead of $data['template'] directly in $template, it loads the dashboard file whereas it doesn’t load when i pass it as string from controller.

dashboard.blade view

<p><?php echo $data['title']; ?></p> // Printing the data from the controller

Solution

  • To include a Blade template into another template, use @include:

    @include('admin.dashboard')
    

    Or

    @include($data['template']) // This should be the name of template, like 'admin.dashboard', but not path
    

    Also, check if the view has the correct name and is in the right directory:

    resources/views/admin/dashboard.blade.php