Home > Enterprise >  trying to insert data into two tables at once in laravel using foreign key
trying to insert data into two tables at once in laravel using foreign key

Time:07-15

Im facing a challenge. I am creating a meeting which is supposed to have members in it. So when creating a meeting the members need to save in their own members table and the meeting in its own table. the migration for the meeting members looks like this

 public function up()
    {
        Schema::create('meeting_members', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string("name");
            $table->string("email");
            $table->unsignedBigInteger('Member_Id')->nullable();
            $table->foreign('Member_Id')->references('id')->on('meeting')->nullOnDelete();
            $table->timestamps();
        });
    }

the migration for the meeting looks like this

 Schema::create('meeting', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->date('date');
            $table->time("from");
            $table->time("to");
            $table->string("title");
            $table->string("agenda");
            $table->string("file_path")->nullable();
            $table->string("location");
            $table->string('description');
            $table->string('guest_name');
            $table->string('guest_email');
            $table->string("comments");
            $table->string("status");
            $table->string("meeting_url");
            $table->string("members");
            $table->string("objective");
            $table->string("topics");
            $table->string("new_business");
            $table->string("new_comments");
            $table->string("previous_meeting");
            $table->date("next_meeting");
            $table->string("publish_status");
            $table->timestamps();
        });

this is the controller to save the meeting and the meeting members into the tables.

$names = json_encode($request->input('name'));
        //dd($meetingMembers);
        $meeting = new Meeting();
        $meeting->date = $request->input("date");
        $meeting->from = $request->input("from");
        $meeting->to = $request->input("to");
        $meeting->title = $request->input("title");
        $meeting->location = $request->input("location");
        $meeting->description = $request->input("description");
        $meeting->guest_name = $request->input("guest_name");
        $meeting->guest_email = $request->input("guest_email"); 
        $meeting->members = json_encode($request->input('name'));
        $meeting->publish_status="yes";
        $meeting->status = "not-done";
        $meeting->save();

        foreach ((array)$names  as $member) {

            $meetingMembers = new MeetingMember();
            $meetingMembers->name = $member;
            $meetingMembers->save();
            echo "done";
           
         }

Bu the challenge is its supposed to add members to a specific meeting based on the foreign key i created.

CodePudding user response:

You need to get started at defining relations in your Models which you need to create first. Checkout the docs for "Eloquent" in the laravel docs, starting here: https://laravel.com/docs/9.x/eloquent (See how models are created, and read about how eloquent does a lot work for you to map the database tables to objects in php)

shortcut:

$ php artisan make:model <your model name>

(checkout the ide-helper to support models even better in your IDE: https://github.com/barryvdh/laravel-ide-helper)

Then: You can define similar relations as on a database level: manyToONe, OneToMay, OneToOne and so on. Checkout the docs for that: https://laravel.com/docs/9.x/eloquent-relationships

shortcut:

<?php
 
namespace App\Models;
 
use Illuminate\Database\Eloquent\Model;
use App\models\MeetingMembers;
 
class Meeting extends Model
{

    protected $fillabel = [<array of writeabel fieldnames>];
//read about mass assignment here: https://laravel.com/docs/9.x/eloquent#mass-assignment
    
    public function meetingMembers()
    {
        return $this->hasMany(MeetingMembers::class); 
//assuming you have a model for meeting members as well
    }
}

When you have set up this, you can create members for the meeting like so:


$meetingModel = //create or aggregate your Meeting

// foreach member:
$meetinModel->MeetingMembers()->create([
   'name' => $memberName, 
   //... and further fields
]);

(further reading: https://laravel.com/docs/9.x/eloquent-relationships#the-create-method)

My short guide here is maybe not complete, so get known with the docs anyways :) but it should kick start your next steps

CodePudding user response:

This should be a many to many relationship. One meeting can have many members and one person can be a member of many meetings.

You need a third table for the relationship that connects meetings with persons. See the laravel documentation: https://laravel.com/docs/9.x/eloquent-relationships#many-to-many

meeting_user
    user_id - integer
    meeting_id - integer

Then you can easily attach new members to meetings or vice versa. See here: https://laravel.com/docs/9.x/eloquent-relationships#attaching-detaching

use App\Models\User;
 
$user = User::find(1);
 
$user->meetings()->attach($meetingId)
  • Related