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);
}
}
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)
Need help with showing posts by category
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:
CategoryTranslation model:
Team model:
TeamTranslation model:
Part of my route:
Part of my TeamController:
And here's my view:
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:
and it shows:
Anyone help?