1
votes

i'm trying to make an blog application with laravel. my problem is that i have listed my posts in allnews.blade.php view and i described texts as routeable to detail pages of post but it doesn't work

allnews.blade.php

@foreach ($posts as $post) // this is allnews.blade.php view
        <li>
            <div class="media wow fadeInDown">
               <a href="{{$post->slug}}" class="container"> <img width="100%" height="auto" alt="" 
               src="/storage/{{ $post->image}}"> </a>
              <div class="media-body"> <h3 src="" class="catg_title">{{$post->title}}</h3> </div>
            </div>
          </li>
        @endforeach

but when i try to go to my post detail(by using href $post->slug ) i'm having issues with "404 page not found" problem. for my logic all things look perfect. but i cant route.

here is my PostController.php

 public function DetailofPost($id)
 {
   $PostDetails  = Post::where('slug' , $id)->first();
   return view('PostDetail', compact ('PostDetails'));
 }

here is my PostDetail.blade.php view

         @foreach($PostDetails as $PostDetail)
    
        <h1>{{PostDetail->title}}</h1>
        <div class="single_page_content"> <img class="img-center" src="/storage/{{ $PostDetail->image}}" alt="">
          <blockquote> {{PostDetail->body}}</blockquote>
         @endforeach    

and here is my routing page a.k.a web.php

  Route::get('/', function () {
 return view('welcome');
                             });
 Route::get("/tumhaberler" , 'PostController@ListAll');

 Route::group(['prefix' => 'admin'], function () {
 Voyager::routes();
                                                 });
 Route::get('/{slug}', 'PostController@DetailofPost');
3
what URL/route returns this allnews.blade.php view? you should avoid relative URLs, this is why we have URL helpers ... this seems like an issue of not knowing how relative URLs work with the current base URL - lagbox

3 Answers

1
votes

Your href should contain a full url, not just your $post->slug.

Change the href to url("/{$post->slug}"), assuming the url you want is something like http://yoursite.com/title-of-my-post

See docs: https://laravel.com/docs/7.x/urls#introduction

1
votes

You're passing the slug into the URL:

<a href="{{$post->slug}}"

but trying to find the post in your controller using ID:

public function DetailofPost($id)

If you want to always use the slug as the identifier, you need to tell Laravel this by changing your route to:

Route::get('/{post:slug}', 'PostController@DetailofPost');

and then change your controller function to automatically load the post with that slug:

public function DetailofPost(App\Post $post)

And then in the controller, you won't need to set $PostDetails as you'll have the variable $post.

This is called Route Model Binding.

-1
votes

Thank you everyone i solved the problem.

     @foreach($PostDetails as $PostDetail) //this is my detail page
     <h1>{{PostDetail->title}}</h1>
    <div class="single_page_content"> <img class="img-center" src="/storage/{{ 
     $PostDetail->image}}" alt="">
     <blockquote> {{PostDetail->body}}</blockquote>
     @endforeach    

my logic was wrong. in my controller i set it like that my controller fetch the id of post from database than fullfill them on detail page by id related database columns like 'slug' or 'title' as you guys see there is no need use @foreach and here is my updated code from PostDetail.blade.php

       <div class="single_page">
        <h1>{{$PostDetails->title}}</h1>
        <div class="single_page_content"> <img class="img-center" src="/storage/{{ 
        $PostDetails->image}}" alt=""> </div>
        <a>  {{$PostDetails->body}} </a>