简体   繁体   中英

Laravel belongsTo relationship returning null?

I'm trying to fetch matches that are scheduled for today, but it seems my relationships aren't working and returning null.

How I fetch todays matches:

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

What's returning null exactly?

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

Also, when trying to foreach $scheduledMatches it throws the null error when accessing fields that I'm trying to access fields on something that is null (a record in $scheduledMatches).

How do I know that it's actually not null?

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

Here is the ClanMatch model:

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');
    }
}

Clan model as requested

<?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);
    }
}

Migrations (table strucuture)

// 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();
});

you can print this code:

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

and check the result in phpmyadmin or other database program.

some debugging tips:

make sure your migrations are like this

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');

now make sure your tables has some data like

clan_matches
id|name|
1|home
2|away

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

now this should return you results

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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