Skip to content
Advertisement

Show.blade.php is not displaying the content from the database, only the layout

`i am having a problem with my show.blade.php template, everything works fine but when I click on a post in the index page it directs me to the /post/1 page without showing the post content only the extended layout. please help

Web.php

Route:: resource('best-practices' ,   'BestpracticesController');

*bestpracticescontroller.php

public function index()
{
        $bestpractices = Bestpractices::all();

        return view('bp.index',compact('bestpractices'));

     }

     public function show(Bestpractices $bestpractices)

     {

        return view('bp.show',compact('bestpractices'));

      }

bp.show view template

@extends('layouts.front')

@section('content')
<div class="blog-details pt-95 pb-100">

    <div class="container">

        <div class="row">

            <div class="col-12">

                <div class="blog-details-info">

                    <div class="blog-meta">

                        <ul>
                        <li>{{$bestpractices->Date}}</li>
                        </ul>

                    </div>

                    <h3>{{$bestpractices->title}} </h3>

                    <img src="{{asset('storage/'.$bestpractices->cover_img)}}" alt="">
                    <div class="blog-feature">

                       {{$bestpractices->body}}

                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Advertisement

Answer

Thats because when you register routes via

Route::resource('best-parctices', BestparcticeController');

//Generated show route is equivalent to 
Route::get(
    '/best-practices/{best_practice}', 
    [BestpracticeController::class, 'show']
);

//route parameter is best_practice

Hence to achieve implicit route model binding the route parameter name must match the parameter name in the controller method

public function show(Bestpractices $bestpractices)

{
    //here $bestpractices will be an int and not an object with 
    //model record as implicit route model binding doesn't work
    return view('bp.show',compact('bestpractices'));
}

public function show(Bestpractices $best_practice)

{
    //here implicit route model binding works so $best_practices is an object 
    //with model record
    return view('bp.show',['bestpractices' => $best_practices]);
}

Or if you don’t want to change the method parameter name in the controller methods then you need to override the route parameter name in the Route:resource() call when you define routes

Route::resource('best-practices',   BestpracticesController::class)
    ->parameters([
        'best-practices' => 'bestpractices'
    ]);

Laravel docs: https://laravel.com/docs/8.x/controllers#restful-naming-resource-route-parameters

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement