F
Filament7mo ago
negro

filament many to many

how to create resource with many to many relationships content additional field ??
9 Replies
Dennis Koch
Dennis Koch7mo ago
What‘s a „many to many content additional field“?!
negro
negroOP7mo ago
i have two models Student and Classroom link with many to many. i have a table with classroom_id, student_id and one additional field(academic_id). hope it's more readable i whish to know how i can field academic_id who is addition field in these relation
Dennis Koch
Dennis Koch7mo ago
I still don’t really understand what’s the issue. But have you looked at relation managers and the repeater? Are you talking about pivot fields?
negro
negroOP7mo ago
this is the error i get: insert into classroom_student (classroom_id, student_id) values (1, 1) SQLSTATE[HY000]: General error: 1364 Field 'academic_id' doesn't have a default value in this relation classroom_student, academic_id is an additional field
Dennis Koch
Dennis Koch7mo ago
Sorry but I can’t help you if you if you just share an error without even knowing what you do. The error says you are missing a required field.
negro
negroOP7mo ago
function in my student model: public function classrooms() { return $this->belongsToMany(Classroom::class)->withPivot('academic_id'); } function in my classroom model: public function students() { return $this->belongsToMany(Student::class,)->withPivot('academic_id'); } student resource form public static function form(Form $form): Form { return $form ->schema([ Forms\Components\Section::make('Information Personnel Apprenant') ->schema([ Forms\Components\TextInput::make('fname') ->label('Nom') ->autofocus() ->required() ->maxLength(255) ->autocapitalize(), Forms\Components\TextInput::make('lname') ->label('Prenom') ->maxLength(255) ->autocapitalize(), Forms\Components\Radio::make('sexe') ->options([ 1 => 'Homme', 0 => 'Femme', ]) ->boolean() ->inline() ->default(1) ->required(), Forms\Components\DatePicker::make('born_at') ->label('Date de Naissance') ->required(), Forms\Components\TextInput::make('born_place') ->label('Lieu de naissance') ->required() ->maxLength(255), Forms\Components\TextInput::make('quarter') ->label('Quartier Habitation') ->maxLength(255) ->required() ]) ->columns(2), ]); } and this class CreateStudent extends CreateRecord { protected static string $resource = StudentResource::class; protected function mutateFormDataBeforeCreate(array $data): array { $data['matricule'] = '122'; $data['academic_id'] = 12; return $data; } }
Dennis Koch
Dennis Koch7mo ago
Please read #✅┊rules about code formatting.
negro
negroOP7mo ago
public function up(): void
{
Schema::create('classrooms', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->foreignId('building_id')->contrained();
$table->foreignId('group_id')->contrained();
$table->foreignId('user_id')->contrained();
$table->text('description');
$table->timestamps();
$table->softDeletes();
});
}
public function up(): void
{
Schema::create('classrooms', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->foreignId('building_id')->contrained();
$table->foreignId('group_id')->contrained();
$table->foreignId('user_id')->contrained();
$table->text('description');
$table->timestamps();
$table->softDeletes();
});
}
public function up(): void
{
Schema::create('students', function (Blueprint $table) {
$table->id();
$table->string('matricule');
$table->string('fname');
$table->string('lname')->nullable();
$table->string('sexe');
$table->string('born_place')->nullable();
$table->string('father_name')->nullable();
$table->string('mother_name');
$table->string('fphone')->nullable();
$table->string('mphone');
$table->date('born_at');
$table->text('allergy')->nullable();
$table->string('logo')->default('anonymous.png');
$table->text('description')->nullable();
$table->string('quarter')->nullable();
$table->timestamps();
$table->softDeletes();
});
}
public function up(): void
{
Schema::create('students', function (Blueprint $table) {
$table->id();
$table->string('matricule');
$table->string('fname');
$table->string('lname')->nullable();
$table->string('sexe');
$table->string('born_place')->nullable();
$table->string('father_name')->nullable();
$table->string('mother_name');
$table->string('fphone')->nullable();
$table->string('mphone');
$table->date('born_at');
$table->text('allergy')->nullable();
$table->string('logo')->default('anonymous.png');
$table->text('description')->nullable();
$table->string('quarter')->nullable();
$table->timestamps();
$table->softDeletes();
});
}
public function up(): void
{
Schema::create('classroom_student', function (Blueprint $table) {
$table->id();
$table->foreignId('student_id')->contrained();
$table->foreignId('classroom_id')->contrained();
$table->foreignId('academic_id')->contrained();
$table->timestamps();
});
}
public function up(): void
{
Schema::create('classroom_student', function (Blueprint $table) {
$table->id();
$table->foreignId('student_id')->contrained();
$table->foreignId('classroom_id')->contrained();
$table->foreignId('academic_id')->contrained();
$table->timestamps();
});
}
my migrations
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Student extends Model
{
use HasFactory, softDeletes;

protected $fillable = [
'matricule',
'fname',
'lname',
'sexe',
'father_name',
'mother_name',
'fphone',
'mphone',
'born_at',
'born_place',
'allergy',
'logo',
'description',
'quarter'
];

protected $dates = [ 'deleted_at' ];

protected $casts = [
'created_at' => 'datetime:Y-m-d',
];

public function classrooms() {
return $this->belongsToMany(Classroom::class)->withPivot('academic_id');
}

public function notes() {
return $this->hasMany(Note::class);
}

}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Student extends Model
{
use HasFactory, softDeletes;

protected $fillable = [
'matricule',
'fname',
'lname',
'sexe',
'father_name',
'mother_name',
'fphone',
'mphone',
'born_at',
'born_place',
'allergy',
'logo',
'description',
'quarter'
];

protected $dates = [ 'deleted_at' ];

protected $casts = [
'created_at' => 'datetime:Y-m-d',
];

public function classrooms() {
return $this->belongsToMany(Classroom::class)->withPivot('academic_id');
}

public function notes() {
return $this->hasMany(Note::class);
}

}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Classroom extends Model
{
use HasFactory, SoftDeletes;

protected $fillable = [
'building_id',
'group_id',
'user_id',
'name',
'description',
];

protected $casts = [
'created_at' => 'datetime:Y-m-d',
];

public function building() {
return $this->belongsTo(Building::class);
}

public function group() {
return $this->belongsTo(Group::class);
}

public function students() {
return $this->belongsToMany(Student::class,)->withPivot('academic_id');
}

public function notes() {
return $this->hasMany(Note::class);
}

public function user() {
return $this->belongsTo(User::class);
}
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Classroom extends Model
{
use HasFactory, SoftDeletes;

protected $fillable = [
'building_id',
'group_id',
'user_id',
'name',
'description',
];

protected $casts = [
'created_at' => 'datetime:Y-m-d',
];

public function building() {
return $this->belongsTo(Building::class);
}

public function group() {
return $this->belongsTo(Group::class);
}

public function students() {
return $this->belongsToMany(Student::class,)->withPivot('academic_id');
}

public function notes() {
return $this->hasMany(Note::class);
}

public function user() {
return $this->belongsTo(User::class);
}
}
my models
negro
negroOP7mo ago
Want results from more Discord servers?
Add your server