数据透视表是最佳解决方案吗?

时间:2017-08-28 05:12:46

标签: php laravel eloquent relational-database

关于透视表和特定功能的最佳实践,我有一个非常基本的问题。

本质上,我有两个实体

球员 问题

我向玩家显示一个问题,当他们回答时,我会根据他们是否正确回答来发起一个奖励他们积分的事件。我现在要做的是创建一个表来存储玩家已回答的问题。所以在这种情况下,我可以将玩家与一个问题联系起来。

之类的,

$player->question()->associate($question);

这将是玩家和问题之间的多对多关系。

我的问题是:

这是实用性和可扩展性方面的最佳解决方案,以及如何获得播放器未在控制器中回答的问题。

基本上与

相反
$player->questions();

如果我要使用多对多的关系

更新

模型 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');
        });
    }
}

1 个答案:

答案 0 :(得分:0)

这里实际上只有两个问题可以回答:玩家可以回答多个问题吗?并且:许多玩家可以回答一个问题吗?如果答案为是,则自动需要数据透视表。这是最好的做法。

相关问题