我在我的應用程序中創建網址友好的,但它不工作,應用程序給我一些與「 - 」有關的問題。 它給我的錯誤:網址友好的路線
ErrorException in PostController.php line 60:
Trying to get property of non-object
我理想中的網址是:
http://domain.com/CATEGORY-title-of-post-ID
我的路線是:
Route::get('{category}-{title}-{id}', '[email protected]');
PostController的顯示功能:
public function show($category,$title,$id)
{
$post = Post::find($id);
$user = Auth::user();
$comments = Comment::where('post_id',$id)
->where('approved',1)
->get();
return view('posts.show',compact('post','comments','user'));
}
刀片查看:
<?php
$title_seo = str_slug($feature->title, '-');
?>
<a href="{{url($feature->categories[0]->internal_name."-".$title_seo."-".$feature->id)}}" rel="bookmark">
...</a>
您好感謝您的答覆,我只是實現它,並且已經將其保存在我的數據庫,但我有一個問題,我怎麼會構建它在我的路線?它仍然給我錯誤。 – Pedro
是否有原因需要使用'{category} - {title} - {id}'?我在想,因爲你是用短劃線分開的,而且可以用短劃線來填充,所以不知道你的類別什麼時候結束和標題開始了。如果你在默認例子中使用'title',你可以將它傳遞給你的控制器,並執行Posts :: where('slug',$ slug) - > first();'。如果你仍然想使用類別,最好是使用斜線('/')分開並在你的路線中反映出來 – TimothyBuktu