【问题标题】:Get Current Category details in Laravel 5.6在 Laravel 5.6 中获取当前类别的详细信息
【发布时间】:2018-12-17 05:07:36
【问题描述】:

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

在我的 ArticleCategory 模型中

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 时,我收到一个错误 "Page Not Found"

当我仅在路由文件中更改 {{ articlecategory }} to {{ category }} 时。它显示一个没有当前类别详细信息的空白页面。如何解决?

注意:我之前在 Laravel 5.5 中使用了相同的代码,它运行良好。在 Laravel 5.6 中,我看到了这个错误。我已经在 chrome 上使用了缓存杀手,并且还清除了 laravel 中的缓存和视图,正如谷歌上的几个链接所建议的那样。但是我仍然看到同样的错误

【问题讨论】:

  • 因为路由模型绑定是使用id查找记录,
  • 你为什么在category 操作中获取相同的ArticleCategory 两次。您已经使用 type-hint 获得了 $articlecategory 对象。
  • 所以你的意思是 $currentcategory = $articlecategory;会做的工作。没看懂
  • 如果去example.com/articles/1看到什么
  • 注意:当 $currentcategory$category 的 smae 时,为什么要使用查询?

标签: laravel laravel-5.6


【解决方案1】:

我找到了一种行之有效的方法。

我已将路由键值更改为 slug 并且它有效。我认为这是由我使用的 slugs 包完成的。将以下代码手动添加到模型后,它按预期工作。

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

【讨论】:

  • 404 因为这一行 public function category(Request $request, ArticleCategory $articlecategory) ,ArticleCategory默认使用id查找记录,所以表示where('id', 'slug'),所以不是找到
  • 它现在可以工作了。我只需要更改 routekey 值。
【解决方案2】:

如果您希望模型绑定在检索给定模型类时使用 id 以外的数据库列,您可以覆盖 Eloquent 模型上的 ‍‍getRouteKeyName 方法:

例如:

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

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

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

还有我的article_category 表:

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

【讨论】:

    【解决方案3】:

    你的路线应该是:

    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');
        });
    

    现在您将 category_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');
       }
     }
    

    【讨论】:

    • 我想当我们在路由中使用 {{ articlecategory }} 时,我应该可以访问与其相关的整个文章类别列。正确的?以及为什么这适用于 5.5 并在 5.6 中显示错误我很困惑
    猜你喜欢
    • 2020-05-06
    • 2021-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多