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....
<?php
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...
<?php
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()
{
Meal::factory()->count(10)->create();
foreach (Meal::all() as $meal) {
$tags = Tag::inRandomOrder()->take(rand(1, 8))->pluck('id');
$ingredients = Ingredient::inRandomOrder()->take(rand(1, 6))->pluck('id');
$meal->tags()->attach($tags);
$meal->ingredients()->attach($ingredients);
}
}
}
My Answer:
specific question or topic you would like to know about PHP?
Rate this post
5 of 5 based on 7525 votesComments