亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我的帖子不會路由到他們的詳細信息頁面

我的帖子不會路由到他們的詳細信息頁面

PHP
夢里花落0921 2023-11-04 21:03:39
我正在嘗試用 laravel 制作一個博客應用程序。我的問題是,我已在 allnews.blade.php 視圖中列出了我的帖子,并且我將文本描述為可路由到帖子的詳細信息頁面,但它不起作用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但是當我嘗試轉到我的帖子詳細信息(通過使用 href $post->slug)時,我遇到了“404 頁面未找到”問題。按照我的邏輯,一切看起來都很完美。但我無法路由。這是我的PostController.php public function DetailofPost($id) {   $PostDetails  = Post::where('slug' , $id)->first();   return view('PostDetail', compact ('PostDetails')); }這是我的PostDetail.blade.php視圖         @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    這是我的路由頁面,又名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 回答

?
蕪湖不蕪

TA貢獻1796條經驗 獲得超7個贊

您將 slug 傳遞到 URL 中:

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

但嘗試使用 ID 在控制器中查找帖子:

public?function?DetailofPost($id)

如果你想始終使用 slug 作為標識符,你需要通過將你的路由更改為以下方式來告訴 Laravel:

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

然后更改控制器功能以自動加載帶有該 slug 的帖子:

public?function?DetailofPost(App\Post?$post)

然后在控制器中,您不需要設置,$PostDetails因為您將擁有變量$post。

這稱為路由模型綁定。


查看完整回答
反對 回復 2023-11-04
?
梵蒂岡之花

TA貢獻1900條經驗 獲得超5個贊

href應該包含完整的網址,而不僅僅是您的$post->slug.

更改hrefurl("/{$post->slug}"),假設您想要的網址類似于http://yoursite.com/title-of-my-post

請參閱文檔: https: //laravel.com/docs/7.x/urls#introduction


查看完整回答
反對 回復 2023-11-04
?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

     @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    

我的邏輯是錯誤的。在我的控制器中,我將其設置為我的控制器從數據庫中獲取帖子的ID,而不是通過ID相關的數據庫列(例如“slug”或“title”)在詳細信息頁面上填充它們,正如你們所看到的,不需要使用@foreach,這里是我從 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>


查看完整回答
反對 回復 2023-11-04
  • 3 回答
  • 0 關注
  • 191 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號