skip to Main Content

I am creating tables in PHPMyAdmin using Laravel Migrations. One of the things that I want to do is have a Question Table and Answers Table where 1 question can have 4 answers (Multiple choice). I want to link the answers and the question with a relationship.

Is it possible to set a primary/foreign key in Laravel in such a way that each time a new Question row is created, the question is connected two 4 answers (when created)?
The rows will be populated manually but they need to be linked.

Is this possible and if so I would appreciate any solutions.

Thank you in advance.

2

Answers


  1. Yes, it’s possible, and here is how
    in the file migration of answers, you may add unsignedBigInteger(question_id).

    in the model Question you may add

    public function answers(){return $this->hasMany(Answer::class)}
    

    in the creation of the answer, you may pass in the request question_id that is related to this answer

    Is that what are you asking for or I misunderstood?

    Login or Signup to reply.
  2. Yes, a common way to do it is to subscribe to the created event for the Question Model. There are several ways to do this, but I usually either register the listeners on booted() in the Model itself or use Observers if I have a lot of event subscriptions going on and want it a little more structured.

    You can register a created listener in a Model like this:

    <?php
    
    namespace AppModels;
    
    use AppModelsAnswer;
    use IlluminateDatabaseEloquentModel;
    use IlluminateDatabaseEloquentRelationsHasMany;
    
    class Question extends Model
    {
        /**
         * The "booted" method of the model.
         *
         * @return void
         */
        protected static function booted()
        {
            static::created(
                function ($question)
                    {
                        // This closure will be executed every time 
                        // a new Question instance has been created
                        $question->Answers()->createMany(
                            [
                                ['sort' => 1],
                                ['sort' => 2],
                                ['sort' => 3],
                                ['sort' => 4],
                            ]
                        );
                    }
            );
        }
    
        /**
         * A Question has many Answers.
         *
         * @return HasMany
         */
        public function Answers(): HasMany
        {
            return $this->hasMany(Answer::class)->orderBy('sort');
        }
    }
    

    In this example, I assumed a sort column in your schema just for demonstration purposes. If you don’t want to prefill any columns on the new Answer instances, you can of course just pass in empty arrays ([]) instead.

    You can read more about this in the docs: https://laravel.com/docs/8.x/eloquent#events

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search