Voyager: Translate frontend?

Created on 1 Aug 2017  路  4Comments  路  Source: the-control-group/voyager

  • Laravel Version: last
  • Voyager Version: last
  • PHP Version: 7
  • Database Driver & Version: mysql last

Description:

Thanks for the great work you are doing, first of all, but I would like to know the method to implement the fields translated in the frontend, any ideas?

Steps To Reproduce:

question

Most helpful comment

Thanks vaggelis2018, this has been enough, in case someone else serves:
Gracias vaggelis2018, con esto ha sido suficiente, por si a alguien m谩s le sirve:

Blade
{{ $post->getTranslatedAttribute('title') }}

Controller:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use TCG\Voyager\Models\Post;

class BlogController extends Controller
{
    public function index() {
        $posts = Post::paginate(5);
        return view('blog.blog', compact('posts'));
    }
}

All 4 comments

In controller

Post::whereFeatured(1)->whereNotNull('image')->get()->translate() 
Voyager::model('Post')->whereFeatured(1)->whereNotNull('image')->get()->translate() 
With this way you translate the whole model but you lose pagination and some other functions like format on timestamps ( Carbon gone ).

Another way is this

$featured_posts = Post::whereFeatured(1)->whereNotNull('image')->get()
$featured_posts = Voyager::model('Post')->whereFeatured(1)->whereNotNull('image')->get()

and in blade
foreach bla bla bla
$featured_post->getTranslatedAttribute('title')
``
with this way you translate only attributes you want and you don't lose any functionalities.

Another approach is this:
$featured_posts = Voyager::model('Post')->whereFeatured(1)->whereNotNull('image')->get();

    if($featured_posts){

        foreach ($featured_posts as $featured_post){
            $featured_post->translation = $featured_post->translate();
        }

        $data['featured_posts'] = $featured_posts;

    }

Not sure if last one i good one

Thanks vaggelis2018, this has been enough, in case someone else serves:
Gracias vaggelis2018, con esto ha sido suficiente, por si a alguien m谩s le sirve:

Blade
{{ $post->getTranslatedAttribute('title') }}

Controller:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use TCG\Voyager\Models\Post;

class BlogController extends Controller
{
    public function index() {
        $posts = Post::paginate(5);
        return view('blog.blog', compact('posts'));
    }
}

Closing as the question has been answered.

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

craigb88 picture craigb88  路  3Comments

MikadoInfo picture MikadoInfo  路  3Comments

wp-src picture wp-src  路  3Comments

iwasherefirst2 picture iwasherefirst2  路  3Comments

zzpwestlife picture zzpwestlife  路  3Comments