Category with Parent_id
public function up(): void
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->foreignId('team_id')->constrained('teams');
$table->string('name');
$table->string('slug')->unique();
$table->foreignId('parent_id')->nullable()->constrained('categories');
$table->string('image')->nullable();
$table->boolean('active')->default(true);
$table->longText('description')->nullable();
$table->timestamps();
});
}
protected $fillable =[
'team_id',
'name',
'slug',
'parent_id',
'image', 'active', 'description', ]; public function team(): BelongsTo { return $this->belongsTo(Team::class); } public function parent(): BelongsTo { return $this->belongsTo(Category::class, 'parent_id'); } public function child():HasMany { return $this->hasMany(Category::class, 'parent_id'); } public function products() : BelongsToMany { return $this->belogsToMany(Product::class); } We get errors like the picture attached.
'image', 'active', 'description', ]; public function team(): BelongsTo { return $this->belongsTo(Team::class); } public function parent(): BelongsTo { return $this->belongsTo(Category::class, 'parent_id'); } public function child():HasMany { return $this->hasMany(Category::class, 'parent_id'); } public function products() : BelongsToMany { return $this->belogsToMany(Product::class); } We get errors like the picture attached.
2 Replies
Error "Field 'team_id' doesn't have a default value" when use select relationship function.
did you find the solution. I have the same problem and I swear I've followed everything.