Answer the question
In order to leave comments, you need to log in
Slug generation via model?
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\Models\Location;
use Illuminate\Support\Str;
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
// $this->call('UserTableSeeder');
$this->call('LocationsSeeder');
}
}
class LocationsSeeder extends Seeder {
public function run()
{
$items = [
['name' => 'Винницкая обл.', 'parent_id' => '0'],
['name' => 'Волынская обл.', 'parent_id' => '0'],
['name' => 'Днепропетровская обл.', 'parent_id' => '0'],
['name' => 'Донецкая обл.', 'parent_id' => '0'],
['name' => 'Житомирская обл.', 'parent_id' => '0'],
['name' => 'Закарпатская обл.', 'parent_id' => '0'],
['name' => 'Запорожская обл.', 'parent_id' => '0'],
['name' => 'Ивано-Франковская обл.', 'parent_id' => '0'],
['name' => 'Киевская обл.', 'parent_id' => '0'],
['name' => 'Кировоградская обл.', 'parent_id' => '0'],
['name' => 'Луганская обл.', 'parent_id' => '0'],
['name' => 'Львовская обл.', 'parent_id' => '0'],
['name' => 'Николаевская обл.', 'parent_id' => '0'],
['name' => 'Одесская обл.', 'parent_id' => '0'],
['name' => 'Полтавская обл.', 'parent_id' => '0'],
['name' => 'Ровненская обл.', 'parent_id' => '0'],
['name' => 'Сумская обл.', 'parent_id' => '0'],
['name' => 'Тернопольская обл.', 'parent_id' => '0'],
['name' => 'Харьковская обл.', 'parent_id' => '0'],
['name' => 'Херсонская обл.', 'parent_id' => '0'],
['name' => 'Хмельницкая обл.', 'parent_id' => '0'],
['name' => 'Черкасская обл.', 'parent_id' => '0'],
['name' => 'Черниговская обл.', 'parent_id' => '0'],
['name' => 'Черновицкая обл.', 'parent_id' => '0'],
];
foreach ($items as $item)
{
Location::create($item);
}
}
public static function boot()
{
parent::boot();
static::saving(function($model) {
$model->slug = str_slug($model->name);
return true;
});
}
}
Answer the question
In order to leave comments, you need to log in
Highly recommend, interesting and understandable documentation on Database: Seeding
It is also worth paying attention to Model Factories and Eloquent events
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question