curved

Experience

140

0 Best Reply Awards

  • Member Since 2 Months Ago
  • 0 Lessons Completed
  • 0 Favorites

17th September, 2017

curved left a reply on 4 Tables Relationship? • 2 months ago

Works perfectly now! Thank you so much! My last question; The answers coming from an array like:

<input name="question_id[1]" value="green">
<input name="question_id[2]" value="red">
<input name="question_id[3]" value="blue">

So is that proper way to update the pivot table this way?

$input = $request->input('question_id');
$user->answers()->detach();
$user->answers()->attach($input);

I also think I don't really need auto increment id on pivot table too, right?

16th September, 2017

curved left a reply on 4 Tables Relationship? • 2 months ago

any suggestions @Snapey ?

15th September, 2017

curved left a reply on 4 Tables Relationship? • 2 months ago

Certainly! :) So I can add more questions from my admin panel.

14th September, 2017

curved left a reply on 4 Tables Relationship? • 2 months ago

I have questions and answer options belongs to each question. Each user can answer this questions and choose different answers. Since I want to be able to add more questions, I created questions and answers tables. What I need is to store user answers in a pivot table and then I want to be able to search users by answers. For example: find users who chose question "favorite color" red. Or get all questions with user's answers

curved left a reply on 4 Tables Relationship? • 2 months ago

Anyone can help me please?

curved left a reply on 4 Tables Relationship? • 2 months ago

Schema::create('questions', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title', 255);
    $table->string('type', 8); // Dropdown, Checkbox, Radio
    $table->integer('sort')->unsigned()->default(0);
    $table->timestamps();
});
        
Schema::create('answers', function (Blueprint $table) {
    $table->increments('id');
    $table->string('text');
    $table->integer('question_id')->unsigned();
    $table->timestamps();

    $table->foreign('question_id')
          ->references('id')
          ->on('questions')
          ->onDelete('cascade');
});

Schema::create('answer_user', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id')->unsigned()->index();
    $table->integer('question_id')->unsigned();
    $table->integer('answer_id')->unsigned()->nullable();
    $table->timestamps();

    $table->foreign('user_id')
          ->references('id')
          ->on('users')
          ->onDelete('cascade');
    
    $table->foreign('question_id')
          ->references('id')
          ->on('questions')
          ->onDelete('cascade');
    
    $table->foreign('answer_id')
          ->references('id')
          ->on('answers')
          ->onDelete('cascade');
});

curved left a reply on 4 Tables Relationship? • 2 months ago

Unknown column 'questions.user_id' I think I'll need my database structure too?

curved left a reply on 4 Tables Relationship? • 2 months ago

Did I understand correctly you mean:

class User extends Authenticatable
{
    public function questions()
    {
        return $this->hasMany('App\Question');
    }
}

class Question extends Model
{
    public function answers()
    {
        return $this->hasMany('App\Answer');
    }

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

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

curved left a reply on 4 Tables Relationship? • 2 months ago

Thank you guys! @tisuchi do you think the table name should be question_user then? I just want to understand Eloquent and use it

curved started a new conversation 4 Tables Relationship? • 2 months ago

Hello people! I am very new to Laravel and watched some videos but pretty confused about Eloquent relationships. Could you please help?

  • 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');
    }
}
Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.