在Laravel 5.6中获取当前类别的详细信息

在Laravel 5.6中获取当前类别的详细信息,laravel,laravel-5.6,Laravel,Laravel 5.6,我正在尝试获取当前类别的详细信息。 例如,如果路由是example.com/articles/category-slug 在我的文章中,分类模型 public function article(){ return $this->hasMany(Article::class); } 文章内模型 public function category(){ return $this->belongsTo(ArticleCategory::class, 'ca

我正在尝试获取当前类别的详细信息。 例如,如果路由是example.com/articles/category-slug

在我的文章中,分类模型

public function article(){
      return $this->hasMany(Article::class);
  }
文章内模型

public function category(){
         return $this->belongsTo(ArticleCategory::class, 'category_id');
    }
路线

Route::group(['prefix' => 'articles'], function(){
      Route::get('/', 'Frontend\ArticleController@index')->name('articles.index');
      Route::get('/{articlecategory}', 'Frontend\ArticleController@category');
      Route::get('/{articlecategory}/{slug}', 'Frontend\ArticleController@show');
    });
物品管理员

public function category(Request $request, ArticleCategory $articlecategory)
 {
   $category = $articlecategory->id;
   $currentcategory = ArticleCategory::where('id', $category)->first();
   return $currentcategory;
 }
我创建了两个类别,1。更新2。新闻

当我转到url
example.com/articles/updates
时,我收到一个错误
“未找到页面”

当我仅在routes文件中将
{{articlecegory}}更改为{{category}}
时。它显示一个空白页,没有当前类别的详细信息。如何解决这个问题


注意:我之前在Laravel5.5中使用了相同的代码,并且运行良好。在Laravel 5.6中,我看到了这个错误。我已经在chrome上使用了一个缓存杀手,并且根据谷歌上的一些链接,我已经清除了laravel中的缓存和视图。但是我仍然看到相同的错误

您的路线应该是:

Route::group(['prefix' => 'articles'], function(){
      Route::get('/', 'Frontend\ArticleController@index')->name('articles.index')->name('articles');
      Route::get('/{category}', 'Frontend\ArticleController@category')->name('category');
      Route::get('/{category}/{slug}', 'Frontend\ArticleController@show')->name('category_articals');
    });
现在您正在向控制器传递一个
类别\u slug

示例:example.com/articles/news

在控制器中:

public function category(Request $request,$category)
 {
   $category = ArticleCategory::where('slug',$category)->first();
   if($category){
      // category found , return category to page
      return view('category',compact('category'));
   }else{
      // category not found , return 404 page
      return view('error.404');
   }
 }

你的路线应该是:

Route::group(['prefix' => 'articles'], function(){
      Route::get('/', 'Frontend\ArticleController@index')->name('articles.index')->name('articles');
      Route::get('/{category}', 'Frontend\ArticleController@category')->name('category');
      Route::get('/{category}/{slug}', 'Frontend\ArticleController@show')->name('category_articals');
    });
现在您正在向控制器传递一个
类别\u slug

示例:example.com/articles/news

在控制器中:

public function category(Request $request,$category)
 {
   $category = ArticleCategory::where('slug',$category)->first();
   if($category){
      // category found , return category to page
      return view('category',compact('category'));
   }else{
      // category not found , return 404 page
      return view('error.404');
   }
 }

我找到了一种有效的方法

我已将路由键值更改为slug,并且成功了。我认为这是由我使用的Slug软件包完成的。在将下面的代码手动添加到模型后,它将按预期工作

public function getRouteKeyName()
      {
          return 'slug';
      }

我找到了一种有效的方法

我已将路由键值更改为slug,并且成功了。我认为这是由我使用的Slug软件包完成的。在将下面的代码手动添加到模型后,它将按预期工作

public function getRouteKeyName()
      {
          return 'slug';
      }

如果希望模型绑定在检索给定模型类时使用id以外的数据库列,则可以重写‍‍<代码>getRouteKeyName雄辩模型上的方法:

例如:

class ArticleCategory extends Model
{
    public $table='article_category';

    public function article(){
        return $this->hasMany(Article::class);
    }

    public function getRouteKeyName()
    {
        return 'category_slug';
    }
}
以及我的
文章类别
表格:

| category_slug |   name  | id |
|:-------------:|:-------:|:--:|
|      cat1     | Updates |  1 |
|      cat2     |   News  |  2 |

如果希望模型绑定在检索给定模型类时使用id以外的数据库列,则可以重写‍‍<代码>getRouteKeyName雄辩模型上的方法:

例如:

class ArticleCategory extends Model
{
    public $table='article_category';

    public function article(){
        return $this->hasMany(Article::class);
    }

    public function getRouteKeyName()
    {
        return 'category_slug';
    }
}
以及我的
文章类别
表格:

| category_slug |   name  | id |
|:-------------:|:-------:|:--:|
|      cat1     | Updates |  1 |
|      cat2     |   News  |  2 |

因为路由模型绑定是使用
id
来查找记录的,为什么在
category
操作中要两次获取相同的
articlegory
。通过使用type-hint,您已经拥有了
$articleCography
对象。因此,您的意思是$currentcography=$articleCography;我会做的。我不明白如果转到
example.com/articles/1
会看到什么注意:当smae作为
$category
时,为什么要使用查询
$currentcategory
?因为路由模型绑定使用
id
来查找记录,为什么在
category
操作中两次获取相同的
articlecegory
。通过使用type-hint,您已经拥有了
$articleCography
对象。因此,您的意思是$currentcography=$articleCography;我会做的。我不明白如果转到
example.com/articles/1
你会看到什么注意:当smae是
$category
时,为什么要使用查询
$currentcategory
?我想当我们在路由中使用{{articleCegory}时,我应该可以访问与之相关的整个文章类别列。对吗?为什么这是5.5版本,并显示了5.6版本的错误,我很困惑,我想当我们在路由中使用{{articleCography}时,我应该可以访问与之相关的整个articleCography列。对吗?我很困惑404,因为这一行是公共函数类别(Request$Request,
articleControle$articleControle
),articleControle默认使用id来查找记录,所以表示where('id','slug'),所以现在不起作用。我只需要更改routekey值。404因为此行是公共函数类别(Request$Request,
articleContegory$articleContegory
),articleContegory默认使用id来查找记录,所以表示where('id','slug'),所以现在不起作用。我只需要更改routekey值。