我有一个关于数据透视表的最基本的问题,以及特定功能的最佳实践。数据透视表是最佳解决方案吗?
从本质上说,我有两个实体
玩家 问题
我展示一个问题的球员,当他们回答,我火了一个事件,以奖励他们点或不取决于他们是否回答正确与否。我现在想要做的是创建一张表来存储玩家已经回答的问题。所以在这个事件中,我可以将玩家与一个问题联系起来。
类似,
$player->question()->associate($question);
这将是一个对很多玩家和问题之间的多对多关系。
在这里我的问题却是:
这是实用性和可扩展性方面的最佳解决方案,以及,我将如何去获得该玩家没有在控制器回答的问题。
基本上
$player->questions();
如果我是使用一个多对多的关系
UPDATE
模型 PlayerProfiles
<?php
namespace App\Models;
use App\Traits\Pointable;
use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;
/**
* Class PlayerProfiles
* @package App\Models
*/
class PlayerProfiles extends Model implements Transformable
{
use TransformableTrait, Pointable;
/**
* @var string
*/
protected $modelName = 'PlayerProfiles';
/**
* @var array
*/
protected $fillable = ['msisdn'];
/**
* @return string
*/
public function getModelName()
{
return $this->modelName;
}
public function questions()
{
return $this->belongsToMany('App\Models\Questions')->withTimestamps();
}
}
问题相反
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;
class Questions extends Model implements Transformable
{
use TransformableTrait, SoftDeletes;
protected $modelName = 'Questions';
protected $fillable = ['question', 'correct_answer', 'incorrect_answer', 'category_id', 'language_id', 'difficulty_level_id'];
public function getModelName()
{
return $this->modelName;
}
public function playerProfiles()
{
return $this->belongsToMany('App\Models\PlayerProfiles')->withTimestamps();
}
}
迁移
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePlayerQuestionsPivotTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('player_questions', function(Blueprint $table)
{
$table->tinyInteger('player_id')->unsigned()->nullable();
$table->tinyInteger('question_id')->unsigned()->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('player_questions');
}
}
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddForeignKeysToPlayerQuestionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('player_questions', function(Blueprint $table)
{
$table->foreign('question_id')->references('id')->on('questions')->onUpdate('RESTRICT')->onDelete('CASCADE');
$table->foreign('player_id')->references('id')->on('player_profiles')->onUpdate('RESTRICT')->onDelete('CASCADE');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('player_questions', function(Blueprint $table)
{
$table->dropForeign('player_questions_questions_id_foreign');
$table->dropForeign('player_questions_player_id_foreign');
});
}
}
你说得对,那是我的一个错字,现在正在更新 –
那么是的,那是最好的解决方案。然后你可以在那里添加答案。然后记录玩家实际回答的内容。 –
很酷,这在我的脑海中似乎是正确的,在获得玩家尚未回答的问题方面,您认为创建运行此查询的特征就足够了吗? –