I am using the laravel-translatable library to develop a multi-language website system. In this web application, there is no front-end and data is read and written through the API. The problem I am facing is that I am not able to get all the records stored in the database in one or more languages, for example from the 'blog' table I am getting all the records with titles English and French. The documentation for this library doesn't explicitly mention this, and I haven't been able to fix it with the code I've tried. Here are code examples I tried, but none solved my problem:
Route::get('/', function () { return response()->json( DB::table('blogs') ->get() ->filter(function ($blog) { return $blog->getTranslations('title', ['en']); }) ); }); Route::get('/', function () { return response()->json( DB::table('blogs') ->get() ->filter(function ($blog) { return collect(json_decode($blog->title))->has('en'); }) ); }); Route::get('/', function () { return response()->json(Blog::titleEqualsEn()->get(), 200); }); span>
You can handle the locale for each request by creating a middleware, like this example:
After that you can easily pass your desired locale as a query string to your endpoint.
Also, make sure to use the Eloquent model instead of the query builder.