简体   繁体   English

Laravel按类别显示帖子

[英]Laravel show posts by category

I'm struggling with this functionality in Laravel 5.5 for a week, so guys please help me to solve this issue. 我在Laravel 5.5的此功能中苦苦挣扎了一周,所以请大家帮我解决这个问题。

In my database I have these tables: 在我的数据库中,有这些表:

categories - id, created_at, updated_at 类别-ID,created_at,updated_at

categories_translations id, category_id, locale, user_id, name, slug, created_at, updated_at Categories_translations id,category_id,语言环境,user_id,名称,子词,created_at,updated_at

teams - id, created_at, updated_at 团队-ID,created_at,updated_at

teams_translations - id, team_id, locale, title, slug, image, body, created_at, updated_at team_translations-id,team_id,语言环境,标题,文档,图像,正文,created_at,updated_at

I'm using Laravel translatable 3rd-party from here: https://github.com/dimsav/laravel-translatable 我正在从这里使用Laravel可翻译的第三方: https : //github.com/dimsav/laravel-translatable

So far I've created relationship successfully with these tables and in my admin section I'm showing created teams with related categories. 到目前为止,我已经成功创建了与这些表的关系,并且在“管理”部分中,我将显示具有相关类别的已创建团队。 Also on frontpage I'm showing all teams in one page list. 同样在首页上,我将所有团队显示在一页列表中。 When I click on specific team, it shows the team page. 当我单击特定团队时,它将显示团队页面。 That's great. 那很棒。

I'm struggling with showing teams by categories. 我正在努力按类别显示团队。

So, this is my code. 所以,这是我的代码。

Category model: 类别模型:

<?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 model: 类别翻译模型:

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

Team model: 团队模式:

<?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 model: 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);
    }
}

Part of my route: 我路线的一部分:

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

Part of my TeamController: 我的TeamController的一部分:

public function teamCategory(Category $category) {

  return $category;
}

And here is my view: 这是我的看法:

@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

For a example I have a category name php, so in the view when I click on the category link, it goes to this url: mywebsite.com/en/team/category/php and it shows: 例如,我有一个类别名称php,因此在视图中单击类别链接时,它会转到以下URL:mywebsite.com/en/team/category/php,它显示:

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

I've tried to add this method in the Category model: 我试图在Category模型中添加此方法:

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

and it shows: 它显示:

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

Later I've tried with this too: 后来我也尝试过这个:

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

but it shows this: 但它显示了这一点:

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

Anyone help? 有人帮忙吗?

EDIT: 编辑:

I've tried Nikola's answer like this: 我已经尝试过Nikola这样的回答:

CategoryTranslation model: 类别翻译模型:

<?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 model: 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);
    }
}

but it shows the same error. 但它显示相同的错误。

Try with updating your 2 sluggable methods in your translate classes to these: 尝试将您的转换类中的2个缓慢的方法更新为以下方法:

CategoryTranslation.php CategoryTranslation.php

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

TeamTranslation.php TeamTranslation.php

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

The above code is based on official docs and should be working with your implementation 上面的代码基于官方文档 ,应与您的实现配合使用

EDIT : I've just noticed you are fetching the wrong entity from your controller, try using this code instead 编辑 :我刚刚注意到您从控制器中获取了错误的实体,请尝试使用此代码

public function teamCategory(CategoryTranslation $category) {

  return $category;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM