簡體   English   中英

Laravel:hasMany Relationship為null / empty

[英]Laravel: hasMany Relationship is null / empty

我的問題是,我的hasMany關系只返回一個“空”集合:

Collection {#172 ▼
  #items: array:1 [▼
    0 => 0
  ]
}

我的belongsTo關系只返回“null”。

以下是雄辯:Game.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Game
 *
 * @property integer $id
 * @property integer $steamid
 * @property string $name
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 */
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;

/**
 * App\Models\Game\Key
 *
 */
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;

/**
 * App\Models\User
 *
 * @property integer $id
 * @property string $name
 * @property string $email
 * @property string $password
 * @property string $remember_token
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 */
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()
    {
        // TODO: not tested yet
        if(count(self::keys()) == 0) return true;
        if(Carbon::now(-24) > self::keys()->first()->created_at) return true;
            return false;
    }
}

這些是我的遷移:

user-table
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    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();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

game-table
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

class CreateGamesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    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();
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //Schema::drop('games');
    }
}

keys-table
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateKeysTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    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();


        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('keys');
    }
}

我用Google搜索了很多,我在Laracast上找到了一些“解決方案”,但他們只告訴我我已有的東西。

有人可以幫忙嗎? Deazl

創建關系的方法必須返回一個值,這就是我所說的:

而不是:

public function keys()
{
    $this->hasMany('App\Models\Game\Key', 'id', 'game_id');
}

你應該有:

public function keys()
{
    return $this->hasMany('App\Models\Game\Key', 'id', 'game_id');
}

不同之處在於return語句,創建關系的方法不返回任何內容,它們應該返回hasOnehasMany的結果,用於所有使用它們進行查詢構建的方法。

PS:您應該為創建關系的其他方法添加return語句。

如果您的中間表中有額外的列,則在定義關系時需要包含它們。

public function keys()
{
    $this->hasMany('App\Models\Game\Key')->withPivot('author_id','redeemer_id')->withTimestamps();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM