Categories: php, mysql, laravel, eloquent

Laravel save data from one form to different database tables

2 answers

I want to create a student with some courses.

This is the Laravel view

I created two different tables: a students table

public function up()     {         Schema::create('students', function (Blueprint $table) {             $table->id();             $table->timestamps();             $table->string('student_name');             $table->string('first_name');             $table->string('last_name');             $table->string('email');         });     } 

and a courses table.

public function up()     {         Schema::create('student_courses', function (Blueprint $table) {             $table->id();             $table->unsignedInteger('student_id');             $table->string('course_name');             $table->timestamps();         });     } 

The students table creates the information of the student and saves it in the student table.

I want to save the courses into the courses table, with the student id. They should have a One to Many relationship. How can I save the different courses into the courses table with the id of the specific student?

All answers to this question, which has the identifier 61205059

The best answer:

You can't store course name in student courses table. You need to create separate tables for courses. After that you can use foreign key for store both the data of student and courses into the student_courses table as per below.

public function up() {     Schema::create('student_courses', function (Blueprint $table) {         $table->id();         $table->integer('student_id')->unsigned()->nullable();         $table->foreign('student_id')->references('id')->on('students')->onDelete('cascade');          $table->integer('course_id')->unsigned()->nullable();         $table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');         $table->timestamps();     }); } 

Hope this will helps you.

First create Relationship: In User Model Create: public function courses(){ return $this->hasMany(Course::class); }

In Course Model Create: public function students(){ return $this->belongsTo(Studnet::class); } Now in your save function:

$course = new Course($request->all()); $user->courses()->save($course); 

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data