jagood
117
2
Laravel

how to change the category translation on the fly?

Posted 1 month ago by jagood

I have displayed posts by category, when I change the language, the category name remains unchanged, while posts change.

PostCategoryController.php

<?php
namespace App\Http\Controllers;
use App\Models\Post;
use App\Models\Photo;
use App\Models\PostCategory;
use App\Repositories\PhotoRepository;
use App\Models\PostTranslation;
use App\Repositories\PostRepository;
use App\Repositories\PostTranslationRepository;

use File;
use Image;
use Illuminate\Http\Request;
class PostController extends Controller
{

    protected $photo;

    public function index()
    {
        $posts = Post::latest()->get();
        return view('post.list', compact('posts'));
    }

    public function show($slug)
    {
        $post = Post::whereTranslation('slug', $slug)->firstOrFail();
        if ($post->translate()->where('slug', $slug)->first()->locale != app()->getLocale()) {
            return redirect()->route('post.show', $post->translate()->slug);
        }
        return view('post.show', compact('post' ));
    }

    public function getPostByCategory(PostRepository $PostRepository, $slug){

        $posts = $PostRepository->getPostByCategory($slug);
        return view('post.list', compact('posts'));
    }

PostRepository.php

<?php

namespace App\Repositories;

use App\Models\Post;

use DB;

class PostRepository extends BaseRepository {

    public function __construct(Post $model){
        $this->model = $model;
    }
    
    public function getPostByCategory($slug){
        return $this->model->where('cena','10')->whereHas('PostCategory',
        function ($query) use ($slug) 
        {
            $query->whereTranslation('slug', $slug);
        })->get();
    }


}

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