Laravel按类别显示帖子

时间:2018-02-09 08:51:08

标签: php laravel categories

我在Laravel 5.5中挣扎了这个功能一周,所以大家请帮我解决这个问题。

在我的数据库中,我有这些表:

类别 - id,created_at,updated_at

categories_translations id,category_id,locale,user_id,name,slug,created_at,updated_at

teams-id,created_at,updated_at

teams_translations - id,team_id,locale,title,slug,image,body,created_at,updated_at

我从这里使用Laravel可翻译的第三方: https://github.com/dimsav/laravel-translatable

到目前为止,我已经成功地与这些表格建立了关系,在我的管理部分中,我展示了创建了相关类别的团队。 同样在首页上我将在一个页面列表中显示所有团队。当我点击特定团队时,它会显示团队页面。太棒了。

我正在努力按类别展示球队。

所以,这是我的代码。

类别模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Dimsav\Translatable\Translatable;

class Category extends Model
{
    use Translatable;

    public $translatedAttributes = ['name', 'slug', 'status', 'user_id', 'image', 'locale'];

    public function teams()
    {
        return $this->hasMany(Team::class);
    } 
}

CategoryTranslation模型:

<?php

namespace App;

use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;

class CategoryTranslation extends Model
{
  use Sluggable;

  protected $touches = ['category'];
  public $timestamps = false;

  /**
   * Return the sluggable configuration array for this model.
   *
   * @return array
   */
  public function sluggable()
  {
      return [
          'slug' => [
              'source' => 'name',
              'onUpdate' => true
          ]
      ];
  }

  public function category() {
    return $this->belongsTo('App\Category');
  }
}

团队模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Dimsav\Translatable\Translatable;

class Team extends Model
{
    use Translatable;

    public $translatedAttributes = ['title', 'slug', 'body', 'status', 'user_id', 'image', 'category_id', 'locale'];

    public function category() {
       return $this->belongsTo(Category::class);
    }
}

TeamTranslation模型:

<?php

namespace App;

use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;

class TeamTranslation extends Model
{
    use Sluggable;

    protected $touches = ['team'];
    public $timestamps = false;

    /**
     * Return the sluggable configuration array for this model.
     *
     * @return array
     */
    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'title',
                'onUpdate' => true,
            ]
        ];
    }

    public function team() {
      return $this->belongsTo(Team::class);
    }
}

我的部分路线:

Route::get('/team/category/{category}', ['uses' => 'TeamController@teamCategory', 'as' => 'teamCategory']);

TeamController的一部分:

public function teamCategory(Category $category) {

  return $category;
}

以下是我的观点:

@extends('layouts.frontend')

@section('title', __('t.team'))

@section('content')
  <div class="main team-list">
    <div class="container">
      <div class="col-md-3">
        <ul>
          @foreach($categories as $category)
            <li><a href="{{ route('teamCategory', $category->slug) }}">{{ $category->name }}</a></li>
          @endforeach
        </ul>
      </div>
      <div class="col-md-9">
        @foreach($teams as $team)
          @if($team->status == 1)
            <div class="row">
              <div class="image">
                <a href="{{ route('team.team', $team->slug) }}"><img width="120" src="{{ Storage::url($team->image) }}" ></a>
              </div>
              <div class="title">
                {{ $team->title }}
              </div>
              <div class="body">
                {!! str_limit($team->body, 300) !!}
              </div>
              <div class="category">
                  {{ $team->category ? $team->category->name : 'No category' }}
              </div>
              <a class="more">More</a>
            </div>
          @endif
        @endforeach
      </div>
    </div>
  </div>
@endsection

举个例子我有一个类别名称php,所以在我点击类别链接的视图中,它会转到这个网址:mywebsite.com/en/team/category/php并显示:

Sorry, the page you are looking for could not be found.

我尝试在类别模型中添加此方法:

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

它显示:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'slug' in 'where clause' (SQL: select * from `categories` where `slug` = php limit 1)

后来我也试过这个:

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

但它显示了这一点:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'where clause' (SQL: select * from `categories` where `name` = php limit 1)

有人帮忙吗?

编辑:

我已经尝试过Nikola的答案:

CategoryTranslation模型:

<?php

namespace App;

use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;

class CategoryTranslation extends Model
{
  use Sluggable;

  protected $touches = ['category'];
  public $timestamps = false;

  /**
   * Return the sluggable configuration array for this model.
   *
   * @return array
   */
  public function sluggable()
  {
      return [
          'slug' => [
              'source' => 'category.slug',
              'onUpdate' => true
          ]
      ];
  }

  public function category() {
    return $this->belongsTo(Category::class);
  }
}

TeamTranslation模型:

<?php

namespace App;

use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;

class TeamTranslation extends Model
{
    use Sluggable;

    protected $touches = ['team'];
    public $timestamps = false;

    /**
     * Return the sluggable configuration array for this model.
     *
     * @return array
     */
    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'team.slug',
                'onUpdate' => true,
            ]
        ];
    }

    public function team() {
      return $this->belongsTo(Team::class);
    }
}

但它显示相同的错误。

1 个答案:

答案 0 :(得分:0)

尝试将翻译类中的2个可缓存方法更新为:

<强> CategoryTranslation.php

/**
 * Return the sluggable configuration array for this model.
 *
 * @return array
 */
  public function sluggable()
  {
    return [
        'slug' => [
            'source' => 'category.slug',
            'onUpdate' => true
        ]
    ];
  }

<强> TeamTranslation.php

/**
 * Return the sluggable configuration array for this model.
 *
 * @return array
 */
  public function sluggable()
  {
    return [
        'slug' => [
            'source' => 'team.slug',
            'onUpdate' => true
        ]
    ];
  }

上述代码基于official docs,应该与您的实施一起使用

编辑:我刚刚注意到您从控制器中取出了错误的实体,请尝试使用此代码

public function teamCategory(CategoryTranslation $category) {

  return $category;
}