John Davidson

php - Is there any 'automatic' way to translate records from database in Laravel?


So I need to translate the records from the database, Im searching a way to do this 'automatic' way but I cannot find any solution.
I'm using astrotomic/laravel-translatable and I created a new Provider for the 'Food' in en_US and in hr_HR.
So I seed the database with both en and hr, but they give random stuff where the translations are not correct.
For example.... Food name is 'pasta' in en(english), and 'pasta' in hr(croatian) is 'tjestenina'...
Can i seed the database with just 'pasta' and do some stuff to translate it automatically or I need to do this by hand.
Here's the code....


namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Category;

class MealFactory extends Factory
* Define the model's default state.
* @return array
public function definition()
# When installed via composer
require_once 'vendor/autoload.php';

$en_faker = \Faker\Factory::create('en_US');
$hr_faker = \Faker\Factory::create('hr_HR');

$title_en = $en_faker->unique()->food;
$title_hr = $hr_faker->unique()->food;

$desc_en = $en_faker->text;
$desc_hr = $hr_faker->text;

return [
'category_id' => rand(null, 5),
'en' => [
'title' => $title_en,
'description' => $desc_en
'hr' => [
'title' => $title_hr,
'description' => $desc_hr
'status' => 'created'


and this is the seeder...


namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Meal;
use App\Models\Tag;
use App\Models\Ingredient;

class MealSeeder extends Seeder
* Run the database seeds.
* @return void
public function run()

foreach (Meal::all() as $meal) {
$tags = Tag::inRandomOrder()->take(rand(1, 8))->pluck('id');
$ingredients = Ingredient::inRandomOrder()->take(rand(1, 6))->pluck('id');

My Answer:
specific question or topic you would like to know about PHP?

Rate this post

3 of 5 based on 9491 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us