emco83

Need help with showing posts by category

Posted 9 months ago by emco83

I'm struggling with this functionality in Laravel for a week, so guys please help me to solve this issue.

In my database I have these tables:

categories - 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, category_id, created_at, updated_at

EDIT: I've forgot to put category_id previously here.

I'm using Laravel translatable 3rd-party from here: 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:

<?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' => '[email protected]', 'as' => 'teamCategory']);

Part of my TeamController:

    public function teamCategory(Category $category) {

      return $category;
    }

And here's 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: Sorry, the page you are looking for could not be found.

I've tried to add this method in the Category model:

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

and it shows:

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

Anyone help?

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.