У меня есть четыре модели со многими ко многим отношениям Пользователи -Я бы -Электронное письмо -Пароль Student -id -Name -школа --------- Тема -Я бы -subname Топи c -Я бы -topName -sub_id -------- Когда они ломаются, производят свои соответствующие сводные таблицы т.е. - student_user -Я бы -Студенческий билет -ID пользователя - student_subject -Я бы -studdent_id -subject_id -subject_name - student_topi c -Я бы -Студенческий билет -topi c -id -topic_name -......... Я хочу вставить их с множественным выбором входных данных, т.е. тема и темы должны быть более одного. Что я потерпел неудачу, так это как вставить их в данные одновременно с таким отношением, поскольку я использовал для каждого l oop и не смог выяснить, какой атрибут я могу использовать ?? Темы и тематические данные в БД уже вставлены
Справка
<table class="table table-striped" id="table_tuition_fee">
<thead>
<tr>
<td><label for="subject">Subject</label></td>
<td><label for="topic">Topic</label></td>
<td><label for="Amount">Amount</label></td>
<td><a class="bg-light btn btn-info addRow" href="#"><i class="fa fa-plus"></i></a></td>
</tr>
</thead>
<tbody>
<tr>
<td>
<select class="form-control subject_selected" name="subject[]" id="subject" data-uid="0">
<option value=" ">---Select Subject---</option>
@foreach($subject_list as $subject)
<option value="{{ $subject->id }}">{{ $subject->SubjectName }}</option>
@endforeach
</select>
</td>
<td>
<select class="form-control topic_selected" name="topic[]" id="topic" data-uid="0">
</select>
</td>
<td>
<input type="text" class="form-control" name="Amount[]" id="Amount"/>
</td>
<td>
<a href="javascript:void(0)" class="btn btn-danger remove">X</a>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td style="border: none"></td>
<td style="border: none"></td>
<td style="border: none"></td>
<td><button class="btn btn-success" id="add_student">SEND</button></td>
</tr>
</tfoot>
</table
class Topic extends Model
{
//
protected $fillable = [
'TopicName'.'Subject_id'
];
public function students(){
return $this->belongsToMany(Student::class)->withTimestamps();
}
public function subject(){
return $this->belongsTo(Subject::class);
}
}
class User extends Authenticatable
{
use Notifiable ,HasRoles;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'FullName'.'EmployeeNumber','password',
];
.
.
.
.
.
public function buyers(){
return $this->belongsToMany(Buyer::class)->withTimestamps();
}
public function students(){
return $this->belongsToMany(User::class)->withTimestamps();
}
}
class Student extends Model
{
//
protected $fillable = [
'FullName'.'School','Combination','Phone','Type','Stream'
];
public function users(){
return $this->belongsToMany(User::class)->withTimestamps();
}
public function subjects(){
return $this->belongsToMany(Subject::class)->withTimestamps();
}
public function topics(){
return $this->belongsToMany(Topic::class)->withTimestamps();
}
}
class Subject extends Model
{
protected $fillable = [
'SubjectName',
];
public function students(){
return $this->belongsToMany(Student::class)->withTimestamps();
}
public function topics(){
return $this->hasMany(Topic::class);
}
}
public function store(Request $request)
{
/*$this->validate($request,[
'FullName' => 'requred|string',
]);*/
$student = new Student();
$student->FullName = $request->input('FullName');
.
.
.
.;
$id = $student->save();
//$user = User::find(auth()->user()->id);
if ($id != 0){
$condition = $request->input('subject');
/*$student->subjects()->create([
'subject_name'=>$request->input('subject')
]);*/
foreach ( $condition as $key => $av){
$data =array([
'subject_id' => $request->input('subject')[$key],
'subject_name' => $request->input('subject')[$key],
'topic_name' => $request->input('topic')[$key],
'Price' => $request->input('Amount')[$key]
]);
$student->subjects()->attach($data);
}
}
return Response()->json($student);
}