Home > Mobile >  store data in different table
store data in different table

Time:10-31

how can i store data to database in different tables

i have two tables in my migration

I want to save the "firstName" to "usersAppointments" table but, it always trying to save the data to "appointments" table

"I'm Beginner"

MIGRATION

    public function up()
    {
        Schema::create('appointments', function (Blueprint $table) {
            $table->id();
            $table->string('vaccine');
            $table->string('venue');        
            $table->date('date');
            $table->timestamps();
        });

        Schema::create('usersAppointments', function (Blueprint $table) {
            $table->id();
            $table->string('firstName');       
            $table->timestamps();
        });
    }

CONTROLLER

            public function store(Request $request){
                
                $data = $request->validate([
                    'vaccine' => 'required',
                    'venue' => 'required',            
                    'date' => 'required'
                ]);
        
                Appointment::create($data);
                return redirect('/admin');
            }

           public function usersAppointment(Request $request){
                
                $data = $request->validate([
                    'firstName' => 'required'               
                ]);
        
                Appointment::create($data);
                return redirect('/');

MODEL

    protected $fillable = [
        'vaccine', 'venue', 'date',
        'firstName'
    ];

CodePudding user response:

That's because to trying insert the data into 'Appointment'.

At First your migration must be separated .

Secondly you must write the code as below :

 public function usersAppointment(Request $request){
            
            $data = $request->validate([
                'firstName' => 'required'               
            ]);
    
            UsersAppointment::create($data);
            return redirect('/');}

CodePudding user response:

Use only one create method in a migration file. When you create a new schema, you must create a new migration file.
Seperate them from each other by another file:

public function up()
{
        Schema::create('appointments', function (Blueprint $table) {
            $table->id();
            $table->string('vaccine');
            $table->string('venue');        
            $table->date('date');
            $table->timestamps();
        });
}

and:

public function up()
{
        Schema::create('usersAppointments', function (Blueprint $table) {
            $table->id();
            $table->string('firstName');       
            $table->timestamps();
        });
}
  • Related