Problem using Many to Many relationship, using filament v2.

SQLSTATE[HY000]: General error: 1364 Field 'category_id' doesn't have a default value
INSERT INTO
`books` (
`image`,
`title`,
`author_id`,
`publication_date`,
`copies`,
`updated_at`,
`created_at`
)
VALUES
(
AxfnWejAwc4eq4h3tIXO88baytGB8F - metaYm9va19ub3VuXzAwMV8wMTY3OS5qcGc = -.jpg,
test,
1,
AS,
AS,
2023 -12 -13 17: 40: 37,
2023 -12 -13 17: 40: 37
)
SQLSTATE[HY000]: General error: 1364 Field 'category_id' doesn't have a default value
INSERT INTO
`books` (
`image`,
`title`,
`author_id`,
`publication_date`,
`copies`,
`updated_at`,
`created_at`
)
VALUES
(
AxfnWejAwc4eq4h3tIXO88baytGB8F - metaYm9va19ub3VuXzAwMV8wMTY3OS5qcGc = -.jpg,
test,
1,
AS,
AS,
2023 -12 -13 17: 40: 37,
2023 -12 -13 17: 40: 37
)
this is my form
public static function form(Form $form): Form
{
return $form
->schema([
Card::make()
->schema([
FileUpload::make('image')->image(),
TextInput::make('title')
->required()
->maxLength(255),
Select::make('author_id')
->relationship('author', 'full_name')
->required(),
Select::make('category_id')
->multiple()
->relationship('categories', 'name')
->preload()
->required(),
TextInput::make('publication_date')
->required()
->maxLength(255),
TextInput::make('copies')
->required()
->maxLength(255)
])
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Card::make()
->schema([
FileUpload::make('image')->image(),
TextInput::make('title')
->required()
->maxLength(255),
Select::make('author_id')
->relationship('author', 'full_name')
->required(),
Select::make('category_id')
->multiple()
->relationship('categories', 'name')
->preload()
->required(),
TextInput::make('publication_date')
->required()
->maxLength(255),
TextInput::make('copies')
->required()
->maxLength(255)
])
]);
}
my models
class Book extends Model
{
use HasFactory;

protected $casts = [
'category_id' => 'array',
];

protected $fillable = ['author_id','image','title','language','publication_date','copies'];

public function author(){
return $this->belongsTo(Author::class);
}

public function categories(){
return $this->belongsToMany(Category::class);
}
}
class Book extends Model
{
use HasFactory;

protected $casts = [
'category_id' => 'array',
];

protected $fillable = ['author_id','image','title','language','publication_date','copies'];

public function author(){
return $this->belongsTo(Author::class);
}

public function categories(){
return $this->belongsToMany(Category::class);
}
}
class Category extends Model
{
use HasFactory;

protected $fillable = ['name'];

public function books(){
return $this->belongsTo(Book::class);
}
}
class Category extends Model
{
use HasFactory;

protected $fillable = ['name'];

public function books(){
return $this->belongsTo(Book::class);
}
}
example input
No description
3 Replies
Killersea
KillerseaOP12mo ago
Im creating a LMS for my project badly need help for this been debugging for hours thank you in advance this is the data shown in the debug
{
"image": {
"c41f8d48-1a70-4805-8a01-df8e0c133302": "livewire-file:gpdZM3zQJ7DfHqNPkjDZ61iVKtPuFu-metaYm9va19ub3VuXzAwMV8wMTY3OS5qcGc=-.jpg"
},
"title": "as",
"author_id": "1",
"category_id": [
"1",
"3",
"2"
],
"publication_date": "as",
"copies": "as"
}
{
"image": {
"c41f8d48-1a70-4805-8a01-df8e0c133302": "livewire-file:gpdZM3zQJ7DfHqNPkjDZ61iVKtPuFu-metaYm9va19ub3VuXzAwMV8wMTY3OS5qcGc=-.jpg"
},
"title": "as",
"author_id": "1",
"category_id": [
"1",
"3",
"2"
],
"publication_date": "as",
"copies": "as"
}
but why is the category_id is not inserting to the category_id or it cant find it here is the migration file
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->id();
$table->foreignId('author_id')->constrained()->cascadeOnDelete();
$table->foreignId('category_id')->constrained()->cascadeOnDelete();
$table->string('image');
$table->string('title');
$table->string('publication_date');
$table->string('copies');
$table->timestamps();
});
}
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->id();
$table->foreignId('author_id')->constrained()->cascadeOnDelete();
$table->foreignId('category_id')->constrained()->cascadeOnDelete();
$table->string('image');
$table->string('title');
$table->string('publication_date');
$table->string('copies');
$table->timestamps();
});
}
awcodes
awcodes12mo ago
Your relationships aren’t right. You can’t cast category_id to an array. Both relationships should be a many to many.
Killersea
KillerseaOP12mo ago
if i change both models relationship to has many it gives me this error Filament\Forms\Components\Select::getRelationship(): Return value must be of type Illuminate\Database\Eloquent\Relations\BelongsTo|Illuminate\Database\Eloquent\Relations\BelongsToMany|Znck\Eloquent\Relations\BelongsToThrough|null, Illuminate\Database\Eloquent\Relations\HasMany returned
Want results from more Discord servers?
Add your server