我在我的应用程序中创建网址友好的,但它不工作,应用程序给我一些与“ - ”有关的问题。 它给我的错误:网址友好的路线
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