ajana.omar
ajana.omar

Reputation: 1

error using Laravel using extends, include and yield

My files are saved in resources\views\layouts as in the picture below :

The code I am using is simple discribed below :

header.blade.php <h1>Header</h1>

footer.blade.php <h1>Footer</h1>

index.blade.php

@include('layouts.header')
@yield('center')
@include('layouts.footer') 

allproducts.blade.php

@extends('layouts.index')
@section('center')
<h1>This is the center of the page</h1>
@endsection

in route web.php

Route::get('Products', ["uses"=>"ProductsController@index"]);

in the controller

<?php

namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class ProductsController extends Controller
{
    public function index()
    {
         return view("allproducts", compact("Products"));
    }
}

I always have this error: View [layout.index] not found.

I included the photo to show that my files are on the right path.

By reading the similar question and proposed solutions asked before about this problem

  1. The view I am including exists in the correct folder.
  2. The file within layouts directory.
  3. In allproducts.balde.php file I wrote @extends('layout.index') instead @extends('layouts.index') as some question solved.

I spent a lot of time trying to fix this problem.

I really appreciate any help

kind regards

Upvotes: 0

Views: 758

Answers (2)

Donkarnash
Donkarnash

Reputation: 12835

@Donkarnash I cleared the view using the command you gave me. I don't know how but the problem is solved. I checked the included file and everything in the code is okay. even my problem solved but I am confused about what was exactly the problem. I tried to add s and remove it as the guys mentioned but the error was the same. otherwise Thank you so much. you really help me

Laravel caches the compiled views. These views are compiled from the blade files which the developer creates.

Sometimes when developer makes some minor changes - like in your case you just added 's' to layout making it layouts - are not picked up by Laravel, the compiled views are not updated i.e. recompiled and cached again.

So at times when you get an error regarding blade views and even after changing/rectifying the error if you still get the same error message - you must clear the cached views by running from the terminal

php artisan view:clear

When you clear the cached views, Laravel recompiles the views from blade files and then caches them

Upvotes: 1

pacoCham
pacoCham

Reputation: 48

If you had a typo in your views, probably you have a typo in your route file

Upvotes: 0

Related Questions