Multiple Text Inputs From Relationship / Saving To Pivot

Ok modifying this question to use a hasMany/pivot relationship. I have a model that has a list of questions and belongs to a career
class CareerQuestion extends Model
{
public function career()
{
return $this->belongsTo(Career::class);
}

public function applications()
{
return $this->belongsToMany(CareerApplication::class, 'applications_questions')
->withPivot('answer')
->withTimestamps();
}
}
class CareerQuestion extends Model
{
public function career()
{
return $this->belongsTo(Career::class);
}

public function applications()
{
return $this->belongsToMany(CareerApplication::class, 'applications_questions')
->withPivot('answer')
->withTimestamps();
}
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class CareerApplication extends Model
{
public function career()
{
return $this->belongsTo(Career::class);
}

public function questions()
{
return $this->belongsToMany(CareerQuestion::class, 'applications_questions')
->withPivot('answer')
->withTimestamps();
}
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class CareerApplication extends Model
{
public function career()
{
return $this->belongsTo(Career::class);
}

public function questions()
{
return $this->belongsToMany(CareerQuestion::class, 'applications_questions')
->withPivot('answer')
->withTimestamps();
}
}
Then I am making a form that will loop through all the questions and give a text input so they can store their answers on the pivot table, I have tried a few different way's however I do not think i am on the right track.
Repeater::make('questions')
->relationship('questions')
->schema([
TextInput::make('answer')
->required(),
])
->itemLabel(fn (array $state): ?string => $state['question'] ?? 'SHOULD NEVER REACH HERE'),
Repeater::make('questions')
->relationship('questions')
->schema([
TextInput::make('answer')
->required(),
])
->itemLabel(fn (array $state): ?string => $state['question'] ?? 'SHOULD NEVER REACH HERE'),
When loading the form the SHOULD NEVER REACH HERE is showing and only 1 evern though there is 4 questions. I am guessing I have the repeater relationship setup wrong ?
1 Reply
datarecall
datarecallOP9mo ago
I also thought about just doing something like this which displays the questions
$inputs = $this->career->questions->map(function ($question) {
return TextInput::make('questions['.$question->id.']')
->label($question->question)
->required();
});
$inputs = $this->career->questions->map(function ($question) {
return TextInput::make('questions['.$question->id.']')
->label($question->question)
->required();
});
but then the $data['questions'] array is messed up
array:6 [▼ // app/Livewire/CareerApplication.php:61
"name" => "1"
"email" => "[email protected]"
"questions[1]" => "3"
"questions[2]" => "4"
"questions[3]" => "5"
"questions[4]" => "6"
]
array:6 [▼ // app/Livewire/CareerApplication.php:61
"name" => "1"
"email" => "[email protected]"
"questions[1]" => "3"
"questions[2]" => "4"
"questions[3]" => "5"
"questions[4]" => "6"
]
Want results from more Discord servers?
Add your server