【问题标题】:Laravel Eloquent trying to retreive all answers with the same foreign key in a tableLaravel Eloquent 试图检索表中具有相同外键的所有答案
【发布时间】:2020-12-04 03:27:55
【问题描述】:

我有一个项目,其中有 3 个表,一个问题表,一个答案表和一个用户表

在问题表中,我有以下内容:

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

在用户表中,我有以下内容:

Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('district')->nullable();
            $table->string('area')->nullable();
            $table->string('committee')->nullable();
            $table->string('position')->nullable();
            $table->rememberToken();
            $table->timestamps();
        });

在答案表中,我有以下内容:

Schema::create('answers', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users');
            $table->unsignedBigInteger('question_id');
            $table->foreign('question_id')->references('id')->on('questions');
            $table->string('answer');
            $table->timestamps();
        });

这些是模型

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

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

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','district','area','committee','position',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

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

根据我的结构,答案表将包含由 user_id 和他对一个问题的回答组成的每一行的条目,在下一行中是另一个问题

如何检索表格中的数据,该表格在第一行显示用户在一列中,他的所有 4 个答案在以下 4 列中?

【问题讨论】:

  • 你的关系在模型中定义了吗?>
  • 是的,我会编辑我的问题

标签: mysql laravel eloquent


【解决方案1】:

第一个Answer 模型应该是这样的。因为它是一对多的。

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

然后你可以得到这样的数据:

$results = User::with('answer')->first();
// this should give the first `user` with all the answers that belongs to the user.

$results = User::with('answer')->get();
// this should give you all the users.

假设您需要所有用户和所有答案。

foreach ($results as $user) {
    echo $user->name;
    
    foreach ($user->answer as $value) {
     echo $value->answer;
   }
}

如果有帮助,请告诉我。

【讨论】:

  • 是的,但如果我需要为每个用户部署他的所有答案,我会使用 $results = User::with('answer')->get() 或 all()?
  • 如果该行应该是 user | 的形式,@foreach 是什么?答案 1 |答案 2 |答案 3 |答案4
  • 还有一件事答案是随机出现的Q1 的答案 1 |为 Q2 2 和下一行回答 2,但对于用户 2
  • @GeorgioBilani 你在说什么,查询中没有question
  • 在 answers 表中,有一个外键用于表问题,用于分配问题的每个答案,因此答案表看起来像 id |用户ID |问题_id |回答我如何根据您在 foreach 中给我的内容按顺序获得 $value->answer,这意味着问题 1-2-3 因为现在我正在接收基于 DB 中的顺序的 $value->answer 所以如果用户按顺序回答问题,如果用户回答问题 2-1-3,则将按顺序显示,则答案不会按顺序显示
猜你喜欢
  • 2020-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
  • 2014-03-11
  • 1970-01-01
  • 2019-10-14
相关资源
最近更新 更多