繁体   English   中英

Laravel属于关系返回null?

[英]Laravel belongsTo relationship returning null?

我正在尝试获取今天安排的匹配项,但似乎我的关系不起作用并返回 null。

我如何获取今天的比赛:

$scheduledMatches = ClanMatch::whereDate('created_at', Carbon::today())->get(); 
// ->count() of the above line is 0 if I add 'has('homeClan', 'awayClan')'

究竟是什么返回 null?

$firstMatch = $scheduledMatches->first();
dd($firstMatch->homeTeam); // returns null

此外,当尝试 foreach $scheduledMatches它会在访问字段时抛出空错误,而这些字段我正试图访问某些内容为空的字段($scheduledMatches 中的记录)。

我怎么知道它实际上不是空的?

$firstMatch = $scheduledMatches->first();
$clan = Clan::find($firstMatch->home_team); // exists

这是 ClanMatch 模型:

class ClanMatch extends Model
{
    protected $table = 'clan_matches';

    public function homeTeam() {
        return $this->belongsTo('App\Clan', 'home_clan');
    }

    public function awayTeam() {
        return $this->belongsTo('App\Clan', 'away_clan');
    }
}

根据要求的氏族模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\ClanMatches;

class Clan extends Model
{
    public function homeMatches() {
        return $this->hasMany('App\ClanMatch', 'home_clan');
    }

    public function awayMatches() {
        return $this->hasMany('App\ClanMatch', 'away_clan');
    }

    public function matches() {
        return ClanMatch::where('home_clan', $this->id)->orWhere('away_clan',$this->id);
    }  

    public function points() {
        return ($this->matchesWon()->count() * 3) + $this->matchesDrawn()->count();
    }

    public function seasons()
    {
        return $this->belongsToMany(Season::class);
    }

    public function tournaments() {
        return $this->belongsToMany(Tournament::class);
    }
}

迁移(表结构)

// clans table
Schema::create('clans', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('tag');
    $table->string('logo');
});

// clan matches table
Schema::create('clan_matches', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('tournament_id');
    $table->integer('home_clan');
    $table->integer('home_score');
    $table->integer('away_clan');
    $table->integer('away_score');
    $table->integer('mode');
    $table->integer('map');
    $table->enum('participants', [4,5,6]);
    $table->timestamp('scheduled_for');
    $table->timestamps();
});

您可以打印此代码:

ClanMatch::with('homeTeam', 'awayTeam')
           ->whereDate('created_at', Carbon::today())
           ->toSql(); 

并在phpmyadmin或其他数据库程序中检查结果。

一些调试技巧:

确保您的迁移是这样的

clan_matches
    $table->increments('id');

clans
    $table->unsignedInteger('home_team')->nullable();
    $table->unsignedInteger('away_team')->nullable();
    $table->foreign('home_team')->references('id')->on('clan_matches');
    $table->foreign('away_team')->references('id')->on('clan_matches');

现在确保你的表有一些数据,比如

clan_matches
id|name|
1|home
2|away

clans
id|name|home_team|away_team
1|home|1|null
2|away|2|null

现在这应该返回你的结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM