I have four models with many to many relationship
Users
-id
-Email
-Password
Student
-id -Name
-school
---------
Subject
-id
-subname
Topic
-id
-topName
-sub_id
--------
When they broke down produce their respective pivot tables
i.e
-student_user
-id
-student_id
-user_id
-student_subject
-id
-studdent_id
-subject_id
-subject_name
-student_topic
-id
-student_id
-topic-id
-topic_name
-.........
I want to insert them with multiple selecting of the inputs i.e subject and topics must more than one. What i failed is how to insert them into a data at the same time with such kind of relation since i used for each loop and i failed to figure out what attribute i can use to?? Help
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);
}via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire