F
Filament2mo ago
negro

filament many to many

how to create resource with many to many relationships content additional field ??
9 Replies
Dennis Koch
Dennis Koch2mo ago
What‘s a „many to many content additional field“?!
negro
negro2mo 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 Koch2mo 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
negro2mo 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 Koch2mo 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
negro2mo 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 Koch2mo ago
Please read #✅┊rules about code formatting.
negro
negro2mo 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
Want results from more Discord servers?
Add your server
More Posts
How to correctly typehint or set return types for IDE support and autocomplete?Hi guys, I create small macro for all fields and entries: ```php // FilamentServiceProvider.php ViegetTabs v3.2.77 error: trim(): Argument #1 ($string) must be of type string, Filament\Support\Enums\Something has changed between Filament v3.2.76 and v3.2.77 with getTabs. In .76, this code works finForm: Nested JSON values are stored as string instead of integersI have a form like: ``` public static function form(Form $form): Form { return $formHow to call a whatsapp chat here?I want to call a WhatsApp Chat in the ->actions() function with the data and clients I chose in my sHello! When im saving my formData - FileUpload gives string like this "icons/01HXW41XREQJ420D95GMW"Hello! When im saving my formData - FileUpload gives string like this "icons/01HXW41XREQJ420D95GMW"LocaleSwitcher label of localeHi, I'm trying to to change the label of the LocaleSwitcher. I have a custom language with the handlRepeater inline delete actionHello, I'm trying to make the repeater delete action inline. Since having the entire header for one ERR_TOO_MANY_REDIRECTS with Multi-Tenancy Using Subdomains and DomainsHello everyone, I'm testing the multi-tenancy option using subdomains and domains. I have configureDefault columns in <resourcename>.phpHi when I first started tinkering with Filament the <modelname>Resource.php file contained referencemodalIs it possible to configure path-based routing in filament with stancyl muti-tenancy ?