我有一個關於數據透視表的最基本的問題,以及特定功能的最佳實踐。數據透視表是最佳解決方案嗎?
從本質上說,我有兩個實體
玩家 問題
我展示一個問題的球員,當他們回答,我火了一個事件,以獎勵他們點或不取決於他們是否回答正確與否。我現在想要做的是創建一張表來存儲玩家已經回答的問題。所以在這個事件中,我可以將玩家與一個問題聯繫起來。
類似,
$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');
});
}
}
你說得對,那是我的一個錯字,現在正在更新 –
那麼是的,那是最好的解決方案。然後你可以在那裏添加答案。然後記錄玩家實際回答的內容。 –
很酷,這在我的腦海中似乎是正確的,在獲得玩家尚未回答的問題方面,您認爲創建運行此查詢的特徵就足夠了嗎? –