Commit b951f44f authored by Jack Stupple's avatar Jack Stupple

make currency source from language

parent 9a0a2fcc
......@@ -6,7 +6,7 @@ class LanguageController extends Controller
{
public function index() {
// raw because <= NOW() is mysql directly into the function and the builder will misunderstand it otherwise
return \App\Language::whereRaw('enabled <= NOW()')->where('enabled', '!=', NULL)->get();
return \App\Language::whereRaw('enabled <= NOW()')->where('enabled', '!=', NULL)->with('currency')->get();
}
public function default() {
......
......@@ -6,10 +6,15 @@ use Illuminate\Database\Eloquent\Model;
class Language extends Model
{
protected $fillable = ['slug', 'name', 'enabled'];
protected $fillable = ['slug', 'name', 'enabled', 'currency_id'];
public function players()
{
return $this->hasMany(Player::class);
}
public function currency()
{
return $this->belongsTo(Currency::class);
}
}
\ No newline at end of file
......@@ -19,11 +19,11 @@ class Player extends Model
'score' => 'float'
];
protected $appends = ['progress', 'questions', 'score', 'score_string', 'season_roundup_position'];
protected $appends = ['progress', 'questions', 'score', 'score_string', 'season_roundup_position', 'currency'];
public function currency()
public function getCurrencyAttribute()
{
return $this->belongsTo('App\Currency');
return $this->language->currency;
}
public function language()
......
<?php
use App\Currency;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
......@@ -170,6 +171,7 @@ class Initial extends Migration
'languages',
function (Blueprint $blueprint) {
$blueprint->increments('id');
$blueprint->integer('currency_id');
$blueprint->string('name');
$blueprint->string('slug');
$blueprint->timestamp('enabled')->nullable();
......@@ -342,9 +344,29 @@ class Initial extends Migration
protected function addSupportedLanguages()
{
Schema::create('currencies', function (Blueprint $table) {
$table->increments('id');
$table->string('currency_code');
$table->timestamps();
});
// default currency
$eur = new Currency();
$eur->currency_code = 'EUR';
$eur->save();
$nok = new Currency();
$nok->currency_code = 'NOK';
$nok->save();
$gbp = new Currency();
$gbp->currency_code = 'GBP';
$gbp->save();
$en = \App\Language::firstOrCreate([
'name' => 'English',
'slug' => 'en'
'slug' => 'en',
'currency_id' => $gbp->id
]);
$en->enabled = $this->created_at;
......@@ -352,7 +374,8 @@ class Initial extends Migration
$nl = \App\Language::firstOrCreate([
'name' => 'Dutch',
'slug' => 'nl'
'slug' => 'nl',
'currency_id' => $eur->id
]);
$nl->enabled = $this->created_at;
......@@ -360,23 +383,26 @@ class Initial extends Migration
$de = \App\Language::firstOrCreate([
'name' => 'German',
'slug' => 'de'
'slug' => 'de',
'currency_id' => $eur->id
]);
// $de->enabled = $this->created_at;
$de->enabled = $this->created_at;
$de->save();
$no = \App\Language::firstOrCreate([
'name' => 'Norwegian',
'slug' => 'no'
'slug' => 'no',
'currency_id' => $nok->id
]);
// $no->enabled = $this->created_at;
$no->enabled = $this->created_at;
$no->save();
$sl = \App\Language::firstOrCreate([
'name' => 'Slovenian',
'slug' => 'sl'
'slug' => 'sl',
'currency_id' => $eur->id
]);
$sl->enabled = $this->created_at;
......@@ -428,5 +454,6 @@ class Initial extends Migration
Schema::dropIfExists('answer_resource_category');
Schema::dropIfExists('resource_resource_category');
Schema::dropIfExists('question_categories');
Schema::dropIfExists('currencies');
}
}
<?php
use App\Currency;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCurrenciesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('currencies', function (Blueprint $table) {
$table->increments('id');
$table->string('currency_code');
$table->timestamps();
});
// default currency
$currency = new Currency();
$currency->currency_code = 'EUR';
$currency->save();
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('currencies');
}
}
<?php
use App\Currency;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddCurrencySupportToPlayers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$currency = Currency::where('currency_code', 'EUR')->firstOrFail();
Schema::table('players', function (Blueprint $table) use ($currency) {
$table->integer('currency_id')->after('language_id')->default($currency->id);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('players', function (Blueprint $table) {
$table->dropColumn('currency_id');
});
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment