在 URL 中显示类别 Slug 而不是 ID
Posted
技术标签:
【中文标题】在 URL 中显示类别 Slug 而不是 ID【英文标题】:Display Category Slug in URL Instead of ID 【发布时间】:2019-05-11 21:28:27 【问题描述】:我对 Laravel 比较陌生,我发现自己一直在尝试显示类别 slug 而不是 ID。
eg: www.website/.../category-slug
我的网站目前显示 www.website/.../category-id。我有一个类别表和一个带有列的帖子表。
posts table = | id | title | body | img | post_category_id|
post_categories table = | id | name | catslug |
控制器
public function getPostCategory($id)
$postCategories = PostCategory::with('posts')
->orderBy('name', 'asc')
->get();
$posts = Post::orderBy('id', 'desc')
->where('post_category_id', $id)
->paginate(5);
return view('articles.category.categoriesposts')->withPosts($posts)->with('postCategories', $postCategories);
路线
Route::get('articles/category/id', [
'uses' => 'ArticlesController@getPostCategory',
'as' => 'pcategory'
]);
我尝试了很多方法,但似乎没有任何效果。任何帮助将不胜感激。
非常感谢,
灰
【问题讨论】:
【参考方案1】:你可以试试这个:
<?php
if(isset($_GET['slug']))
$get_slug = $_GET['slug'];
$query = "SELECT * FROM `table_name` WHERE get_slug ='$get_slug'";
if($result = mysqli_query($conn, $query))
$posts = mysqli_fetch_array($result);
?>
如果你想显示结果,你可以试试这个代码。
<?php echo $posts['get-slug']; ?>
【讨论】:
【参考方案2】:这应该适合你:
**ROUTE:**
Route::get('articles/category/slug', [
'uses' => 'ArticlesController@getPostCategory' ,
'as' => 'pcategory'
] );
**CONTROLLER**
public function getPostCategory($slug)
$postCategories = PostCategory::with('posts')
->orderBy('name', 'asc')
->get();
$posts = Post::orderBy('id', 'desc')
->whereHas('post_category', function ($query) use ($slug)
$query->where('catslug', 'like', $slug);
)->paginate(5);
// return view
return view ('articles.category.categoriesposts')->withPosts($posts)->with('postCategories', $postCategories);
【讨论】:
我收到以下错误 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'catslug' in 'where clause' (SQL: select count(*) as aggregate fromposts
where @987654324 @LIKE剧)
SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列'catslug'(SQL:从posts
中选择计数(*)作为聚合,其中catslug
LIKE 音乐)
【参考方案3】:
ArticlesController.php
public function getPostCategory($slug)
$postCategories = PostCategory::with('posts')
->orderBy('name', 'asc')
->where('catslug', '=', $slug)
->first();
// $postCategories->posts - already is a collection of your posts related only to the category you're looking for
// return view
return view ('articles.category.categoriesposts')->with('postCategories', $postCategories);
Route::get('articles/category/slug', [
'uses' => 'ArticlesController@getPostCategory' ,
'as' => 'pcategory'
] );
就是这样。此外,您可以缩小路线代码:
Route::get('articles/category/slug', 'ArticlesController@getPostCategory')->name('pcategory');
【讨论】:
我已经尝试过这个我得到以下错误 ErrorException (E_ERROR) Undefined variable: posts (View: /Users/macpro/sites/sixmedia/resources/views/articles/category/categoriesposts.blade.php ) 问题是我正在尝试按所选类别循环帖子,所以我需要访问帖子表以循环数据,我还需要访问类别表以获取该类别的 slug 你不需要在你的 categoryposts.blade.php 中使用$posts
。使用$postCategories->posts
就足够了 - 这是您可以循环访问的集合。 @foreach($postCategories->posts as $post) $post->title @endforeach
它加载没有错误,但它不会循环帖子或类别菜单..以上是关于在 URL 中显示类别 Slug 而不是 ID的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Magento 中使用产品 URL 附加类别和子类别 slug