My problem is that my hasMany relationships only return an “empty” collection:
Collection {
0 => 0
]
}
My belongsTo relationship just returns "null".
Here's the eloquence: Game.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Game extends Model
{
protected $fillable = [
'appid', 'name'
];
public function keys()
{
$this->hasMany('App\Models\Game\Key', 'id', 'game_id');
}
}
Key.php
<?php
namespace App\Models\Game;
use Illuminate\Database\Eloquent\Model;
class Key extends Model
{
protected $fillable = [
'key', 'game_id', 'author_id',
];
public static function getRandom()
{
if (!self::available()) return false;
$keys = Key::where('redeemer_id', 0)->get();
$key = $keys[ rand(0, count($keys) - 1) ];
return $key;
}
public static function available()
{
$keys = Key::where('redeemer_id', 0)->get();
$count = count($keys);
if ($count > 0) return $count;
return false;
}
public function author()
{
$this->hasOne('App\Models\User', 'author_id');
}
public function redeemer()
{
$this->hasOne('App\Models\User', 'redeemer_id');
}
public function game()
{
$this->belongsTo('App\Models\Game', 'game_id');
}
}
User.php
<?php
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
protected $fillable = [
'username', 'email', 'password'
];
protected $hidden = [
'password', 'remember_token',
];
public function keys()
{
$this->hasMany('App\Models\Game\Key');
}
public function canRedeem()
{
if(count(self::keys()) == 0) return true;
if(Carbon::now(-24) > self::keys()->first()->created_at) return true;
return false;
}
}
these are my migrations:
user-table
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::drop('users');
}
}
game-table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateGamesTable extends Migration
{
public function up()
{
if (!Schema::hasTable('games')) {
Schema::create('games', function (Blueprint $table) {
$table->increments('id');
$table->integer('appid')->unsigned();
$table->string('name');
$table->timestamps();
});
}
}
public function down()
{
}
}
keys-table
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateKeysTable extends Migration
{
public function up()
{
Schema::create('keys', function(Blueprint $table) {
$table->increments('id');
$table->string('key');
$table->integer('game_id')->unsigned()->index();
$table->foreign('game_id')->references('id')->on('games')->onDelete('cascade');
$table->integer('author_id')->unsigned()->index();
$table->integer('redeemer_id')->unsigned()->index();
$table->timestamps();
});
}
public function down()
{
Schema::drop('keys');
}
}
I searched a lot on googled and I found some “solutions” on Laracast, but they only tell me what I already have.
Can anyone help? Deazl
source
share