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。
這稱為路由模型綁定。
TA貢獻1900條經驗 獲得超5個贊
您href應該包含完整的網址,而不僅僅是您的$post->slug.
更改href為url("/{$post->slug}"),假設您想要的網址類似于http://yoursite.com/title-of-my-post
請參閱文檔: https: //laravel.com/docs/7.x/urls#introduction
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>
- 3 回答
- 0 關注
- 211 瀏覽
添加回答
舉報
