Home > Net >  laravel view doesn't show data after adding new route
laravel view doesn't show data after adding new route

Time:03-15

It works really good, but when I've created new page something goes wrong and now my blog view doesn't show data, but blogs view still correctly show data . I am trying to show detailed data of each blog when user click on button "Details"

MainController:

  public function blog(Blogs $blog)
   {

       return view('blog', compact('blog'));
   }
   public function blogs()
   {
       return view('blogs',['blogs' => Blogs::all(),]);
   }

blogs.blade.php:

@extends('layouts.master')

@section('title', __('main.blogs'))

@section('content')
   <div >
       @foreach($blogs as $blog)
           @include('layouts.cardBlog', compact('blog'))
       @endforeach
   </div>
@endsection

and cardBlog.blade.php:

<div >
   <div >
       <img src="{{($blog->image) }}">
       <div >
           <h3>{{ $blog->title }}</h3>
           <p>{{  $blog->body }}</p>
           <p>
               <a href="{{route('blog', $blog->id) }}"
                  
                  role="button">@lang('main.more')</a>
               @csrf
           </p>
       </div>
   </div>
</div>

blog.blade.php

@extends('layouts.master')

@section('title', __('main.blogs'))

@section('content')
   <h1>{{ $blog->title}}</h1>

   <img src="{{$blog->image }}">
   <p>{{ $blog->body }}</p>
@endsection

web.php:

 Route::get('/', 'MainController@index')->name('index');

   Route::get('/categories', 'MainController@categories')->name('categories');
   Route::get('/about', 'MainController@aboutus')->name('about');
   Route::get('/contact-us', 'ContactUSController@contactUS')->name('contact-us');
   Route::post('contactus', ['as'=>'contactus.store','uses'=>'ContactUSController@contactSaveData']);
   Route::get('/contacts', 'MainController@contacts')->name('contacts');


   Route::get('/blogs', 'MainController@blogs')->name('blogs');
   Route::get('/blog/{id}', 'MainController@blog')->name('blog');


   Route::get('/intership', 'MainController@intership')->name('intership');
   Route::get('/{category}', 'MainController@category')->name('category');
   Route::get('/{category}/{product}/{skus}', 'MainController@sku')->name('sku');
   Route::post('subscription/{skus}', 'MainController@subscribe')->name('subscription');

CodePudding user response:

What's is the error that it shown?

I suggest use "compact" in this line code

return view('blogs',['blogs' => Blogs::all(),]);

Something like this

public function blogs()
{
    $blogs = Blogs::all();
    return view('blogs',compact('blogs'));
}

CodePudding user response:

Try this

public function blog($id)
{
   $blog = Blogs::findOrFail($id)
    return view('blog', compact('blog'));
}
  • Related