繁体   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