Laravel 8 - 当工厂中的自引用关系时,该过程已用信号“11”发出信号
Posted
技术标签:
【中文标题】Laravel 8 - 当工厂中的自引用关系时,该过程已用信号“11”发出信号【英文标题】:Laravel 8 - The process has been signaled with signal "11" when self referencing relationships in factories 【发布时间】:2021-02-14 10:19:46 【问题描述】:我收到以下错误“进程已收到信号“11”的信号。”在我的 CategoryFactory 中执行自引用关系时。例如我有以下代码:
CategoryTest.php
<?php
namespace Tests\Unit\Models\Categories;
use App\Models\Category;
use Tests\TestCase;
class CategoryTest extends TestCase
/**
* A basic unit test example.
*
* @return void
*/
public function test_it_has_children()
$category = Category::factory()
->has(Category::factory()->count(3), 'children')
->create();
$this->assertInstanceOf(Category::class, $category->children->first());
看起来这是错误的来源。我现在确定这是否是引用 hasMany 关系的正确方法。
CategoryFactory.php
<?php
namespace Database\Factories;
use App\Models\Category;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class CategoryFactory extends Factory
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Category::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
return [
'name' => $name = $this->faker->unique()->name,
'slug' => Str::of($name)->slug('-'),
'parent_id' => Category::factory() // This seems to be causing the error
];
Category.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
use HasFactory;
protected $fillable = [
'name',
'slug',
'order'
];
public function children()
return $this->hasMany(Category::class, 'parent_id', 'id');
我不知道这个错误可能指向什么
【问题讨论】:
【参考方案1】:关于这条线,您认为这似乎会导致错误。 我认为错误可能是由递归或类似的东西引起的。 尝试将其更改为:
'parent_id' => function ()
return factory(Category::class)->create()->id;
【讨论】:
以上是关于Laravel 8 - 当工厂中的自引用关系时,该过程已用信号“11”发出信号的主要内容,如果未能解决你的问题,请参考以下文章