簡體   English   中英

Laravel按類別顯示帖子

[英]Laravel show posts by category

我在Laravel 5.5的此功能中苦苦掙扎了一周,所以請大家幫我解決這個問題。

在我的數據庫中,有這些表:

類別-ID,created_at,updated_at

Categories_translations id,category_id,語言環境,user_id,名稱,子詞,created_at,updated_at

團隊-ID,created_at,updated_at

team_translations-id,team_id,語言環境,標題,文檔,圖像,正文,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);
    } 
}

類別翻譯模型:

<?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,因此在視圖中單擊類別鏈接時,它會轉到以下URL:mywebsite.com/en/team/category/php,它顯示:

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

我試圖在Category模型中添加此方法:

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這樣的回答:

類別翻譯模型:

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

但它顯示相同的錯誤。

嘗試將您的轉換類中的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
        ]
    ];
  }

上面的代碼基於官方文檔 ,應與您的實現配合使用

編輯 :我剛剛注意到您從控制器中獲取了錯誤的實體,請嘗試使用此代碼

public function teamCategory(CategoryTranslation $category) {

  return $category;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM