Мне нужно скопировать записи из DeCategories
в Categories
(около 1,7 миллиона) вместе с «обработкой» (новый slug et c).
Использование категории: https://packagist.org/packages/kalnoy/nestedset
Следующий сценарий работает нормально, но работает много дней. Как оптимизировать / заставить работать быстрее? Я новичок в Laravel.
У меня есть эти модели
class DeCategories extends Model
{
use ScopeActiveTrait;
protected $quarded = ['id'];
protected $fillable = ['category_name', 'description', 'keywords', 'content', 'enable', 'photo', 'order', 'slug', '_lft', '_rgt', 'parent_id'];
public $timestamps = true;
protected $table = 'cms_multikategorie22';
}
class Category extends Model
{
use ScopeActiveTrait;
use NodeTrait;
use Slugable;
public function setCategoryNameAttribute($value)
{
$this->attributes['category_name'] = $value;
$this->attributes['slug'] = $this->makeSlug($value);
}
protected $quarded = ['id'];
protected $fillable = ['category_name', 'description', 'keywords', 'content', 'enable', 'photo', 'order', 'slug', '_lft', '_rgt', 'parent_id'];
public $timestamps = false;
}
trait Slugable
{
protected function makeSlug($value)
{
$slug = str_slug($value);
$count = static::whereRaw("slug RLIKE '^{$slug}(-[0-9]+)?$'")->count();
return $count ? "{$slug}-{$count}" : $slug;
}
}
trait ScopeActiveTrait
{
public function scopeActive($query)
{
return $query->where('enable', 1);
}
}
И контроллер:
public function index()
{
echo 'Importuje <br/>';
//die();
$level1 = DeCategories::where('title', "<>", '')->where('parent_id', 325193)->get();
if (!$level1->isEmpty()) {
foreach ($level1 as $key1 => $value1) {
echo $value1->title . "<br/>";
////////
$slug = str_slug($value1->title);
$next = 1;
while (Category::where('slug', '=', $slug)->exists()) {
$next++;
$slug = str_slug($value1->title . '-' . $next);
}
////////
$category = new Category();
$category->category_name = $value1->title;
$category->description = $value1->title;
$category->keywords = $value1->title;
$category->content = $value1->id . '-1';
$category->enable = 1;
$category->photo = '';
$category->order = $value1->position;
$category->parent_id = 0;
$category->slug = $slug;
$category->save();
$id1 = $category->id;
$level2 = DeCategories::where('parent_id', $value1->id)->get();
//dump($level2);
if (!$level2->isEmpty()) {
foreach ($level2 as $key2 => $value2) {
////////
$slug2 = str_slug($value2->title);
$next2 = 1;
while (Category::where('slug', '=', $slug2)->exists()) {
$next2++;
$slug2 = str_slug($value2->title . '-' . $next2);
}
////////
$category2 = new Category();
$category2->category_name = $value2->title;
$category2->description = $value2->title;
$category2->keywords = $value2->title;
$category2->content = $value2->id . '-2';
$category2->enable = 1;
$category2->photo = '';
$category2->order = $value2->position;
$category2->parent_id = $id1;
$category2->slug = $slug2;
$category2->save();
$id2 = $category2->id;
$level3 = DeCategories::where('parent_id', $value2->id)->get();
if (!$level3->isEmpty() && $value2->id !="" && $value2->id != null) {
foreach ($level3 as $key3 => $value3) {
////////
$slug3 = str_slug($value3->title);
$next3 = 1;
while (Category::where('slug', '=', $slug3)->exists()) {
$next3++;
$slug3 = str_slug($value3->title . '-' . $next3);
}
////////
$category3 = new Category();
$category3->category_name = $value3->title;
$category3->description = $value3->title;
$category3->keywords = $value3->title;
$category3->content = $value3->id . '-3';
$category3->enable = 1;
$category3->photo = '';
$category3->order = $value3->position;
$category3->parent_id = $id2;
$category3->slug = $slug3;
$category3->save();
$id3 = $category3->id;
$level4 = DeCategories::where('parent_id', $value3->id)->get();
if (!$level4->isEmpty() && $value3->id !="" && $value3->id != null) {
foreach ($level4 as $key4 => $value4) {
////////
$slug4 = str_slug($value4->title);
$next4 = 1;
while (Category::where('slug', '=', $slug4)->exists()) {
$next4++;
$slug4 = str_slug($value4->title . '-' . $next4);
}
////////
$category4 = new Category();
$category4->category_name = $value4->title;
$category4->description = $value4->title;
$category4->keywords = $value4->title;
$category4->content = $value4->id . '-4';
$category4->enable = 1;
$category4->photo = '';
$category4->order = $value4->position;
$category4->parent_id = $id3;
$category4->slug = $slug4;
$category4->save();
$id4 = $category4->id;
$level5 = DeCategories::where('parent_id', $value4->id)->get();
if (!$level5->isEmpty() && $value4->id !="" && $value4->id != null) {
foreach ($level5 as $key5 => $value5) {
$slug5 = str_slug($value5->title);
$next5 = 1;
while (Category::where('slug', '=', $slug5)->exists()) {
$next5++;
$slug5 = str_slug($value5->title . '-' . $next5);
}
$category5 = new Category();
$category5->category_name = $value5->title;
$category5->description = $value5->title;
$category5->keywords = $value5->title;
$category5->content = $value5->id . '-5';
$category5->enable = 1;
$category5->photo = '';
$category5->order = $value5->position;
$category5->parent_id = $id4;
$category5->slug = $slug5;
$category5->save();
$id5 = $category5->id;
$level6 = DeCategories::where('parent_id', $value5->id)->get();
if (!$level6->isEmpty() && $value5->id !="" && $value5->id != null) {
foreach ($level6 as $key6 => $value6) {
$slug6 = str_slug($value6->title);
$next6 = 1;
while (Category::where('slug', '=', $slug6)->exists()) {
$next6++;
$slug6 = str_slug($value6->title . '-' . $next6);
}
$category6 = new Category();
$category6->category_name = $value6->title;
$category6->description = $value6->title;
$category6->keywords = $value6->title;
$category6->content = $value6->id . '-6';
$category6->enable = 1;
$category6->photo = '';
$category6->order = $value6->position;
$category6->parent_id = $id5;
$category6->slug = $slug6;
$category6->save();
$id6 = $category6->id;
$level7 = DeCategories::where('parent_id', $value6->id)->get();
if (!$level7->isEmpty() && $value6->id !="" && $value6->id != null) {
foreach ($level7 as $key7 => $value7) {
$slug7 = str_slug($value7->title);
$next7 = 1;
while (Category::where('slug', '=', $slug7)->exists()) {
$next7++;
$slug7 = str_slug($value7->title . '-' . $next7);
}
$category7 = new Category();
$category7->category_name = $value7->title;
$category7->description = $value7->title;
$category7->keywords = $value7->title;
$category7->content = $value7->id . '-7';
$category7->enable = 1;
$category7->photo = '';
$category7->order = $value7->position;
$category7->parent_id = $id6;
$category7->slug = $slug7;
$category7->save();
}
}
}
}
}
}
}
}
}
}
}
}
}
}
echo 'finish';
}