0

Used this Spatie/laravel-translatable package i achived my localization with ORM queries like (Article::all();). But when i use raw query or join operations(DB queries), it shows the raw json from database.

Article.php

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;
use Spatie\Translatable\HasTranslations;

class Article extends Model
{
    use HasTranslations;

    public $translatable = ['name'];
}

?>

Controller

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App;
use App\Article;
use App\User;

class TestController extends Controller
{
    public function index()
    {
        return $result = DB::table('articles')->get();
    }
}
?>
apokryfos
  • 38,771
  • 9
  • 70
  • 114
Manoj R
  • 3
  • 5

2 Answers2

1

I had the same problem and my solution (the only way I found) was define an accessor to get the attribute translated:

public function getNameAttribute($value)
{
    return json_decode($value)->{\App::getLocale()};
}
xavidejuan
  • 423
  • 5
  • 7
0

You are getting them trough the DB facade, therefor you get raw data from the database via Query Builder.

To get the translations you should query the articles table through the Article model making use of Eloquent ORM instead.

public function index()
{
    return Article::get();
}

If you want to join some data by hand, you can:

Article::query()
    ->join('table', 'table.article_id', 'articles.id')
    ->get();
reppair
  • 313
  • 5
  • 11