Support the ongoing development of Laravel.io →
Eloquent Database
Last updated 2 years ago.
0

You can use this

users
-----
id
name
...

questions
---------
id
title
answers
...

user_answers
------------
id
question_id
answers ( this answers user will compare with table question field answers )
user_id

or this

users
-----
id
name
...

questions
---------
id
title
...

answers
-------
id
question_id
answers

user_answers
------------
id
question_id
answers ( this answers user will compare with table answers field answers )
user_id
Last updated 7 years ago.
0

Now I have this but not sure if relationships are correct. How can I update or insert if answers are nor exist in my answer_user table?

  • users:
    • id
    • username
  • questions:
    • id
    • title
  • answers:
    • id
    • question_id
    • text
  • answer_user:
    • id
    • user_id
    • question_id
    • answer_id
class Question extends Model
{

    public function answer()
    {
        return $this->hasMany('App\Answer');
    }
}

class Answer extends Model
{

    public function question()
    {
        return $this->belongsTo('App\Question');
    }

}
class User extends Authenticatable
{
    public function answers()
    {
        return $this->belongsToMany('App\Answer');
    }
}


Last updated 7 years ago.
0

Anyone can help me for the correct relationship please?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Curved curvedb Joined 13 Sep 2017

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.