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('AppClan', 'home_clan');
}
public function awayTeam() {
return $this->belongsTo('AppClan', 'away_clan');
}
}
Clan model as requested
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
use AppClanMatches;
class Clan extends Model
{
public function homeMatches() {
return $this->hasMany('AppClanMatch', 'home_clan');
}
public function awayMatches() {
return $this->hasMany('AppClanMatch', '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();
});
php laravel
|
show 3 more comments
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('AppClan', 'home_clan');
}
public function awayTeam() {
return $this->belongsTo('AppClan', 'away_clan');
}
}
Clan model as requested
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
use AppClanMatches;
class Clan extends Model
{
public function homeMatches() {
return $this->hasMany('AppClanMatch', 'home_clan');
}
public function awayMatches() {
return $this->hasMany('AppClanMatch', '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();
});
php laravel
What's the output ofcount($scheduledMatches);
?
– Mozammil
Jan 20 at 14:34
Its 0 if I addhas('homeClan', 'awayClan')
, 1 if I don't add thehas('homeClan', 'awayClan')
.
– Lee Nelson
Jan 20 at 14:35
1
Could we also see yourAppClan
model? Particularly the relationships.
– Mozammil
Jan 20 at 14:38
Updated the question with my Clan model.
– Lee Nelson
Jan 20 at 14:47
Hmm, tough to figure it out without seeing your table structure. Would you be able to post that as well?
– Mozammil
Jan 20 at 15:00
|
show 3 more comments
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('AppClan', 'home_clan');
}
public function awayTeam() {
return $this->belongsTo('AppClan', 'away_clan');
}
}
Clan model as requested
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
use AppClanMatches;
class Clan extends Model
{
public function homeMatches() {
return $this->hasMany('AppClanMatch', 'home_clan');
}
public function awayMatches() {
return $this->hasMany('AppClanMatch', '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();
});
php laravel
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('AppClan', 'home_clan');
}
public function awayTeam() {
return $this->belongsTo('AppClan', 'away_clan');
}
}
Clan model as requested
<?php
namespace App;
use IlluminateDatabaseEloquentModel;
use AppClanMatches;
class Clan extends Model
{
public function homeMatches() {
return $this->hasMany('AppClanMatch', 'home_clan');
}
public function awayMatches() {
return $this->hasMany('AppClanMatch', '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();
});
php laravel
php laravel
edited Jan 20 at 15:26
Lee Nelson
asked Jan 20 at 14:27
Lee NelsonLee Nelson
438
438
What's the output ofcount($scheduledMatches);
?
– Mozammil
Jan 20 at 14:34
Its 0 if I addhas('homeClan', 'awayClan')
, 1 if I don't add thehas('homeClan', 'awayClan')
.
– Lee Nelson
Jan 20 at 14:35
1
Could we also see yourAppClan
model? Particularly the relationships.
– Mozammil
Jan 20 at 14:38
Updated the question with my Clan model.
– Lee Nelson
Jan 20 at 14:47
Hmm, tough to figure it out without seeing your table structure. Would you be able to post that as well?
– Mozammil
Jan 20 at 15:00
|
show 3 more comments
What's the output ofcount($scheduledMatches);
?
– Mozammil
Jan 20 at 14:34
Its 0 if I addhas('homeClan', 'awayClan')
, 1 if I don't add thehas('homeClan', 'awayClan')
.
– Lee Nelson
Jan 20 at 14:35
1
Could we also see yourAppClan
model? Particularly the relationships.
– Mozammil
Jan 20 at 14:38
Updated the question with my Clan model.
– Lee Nelson
Jan 20 at 14:47
Hmm, tough to figure it out without seeing your table structure. Would you be able to post that as well?
– Mozammil
Jan 20 at 15:00
What's the output of
count($scheduledMatches);
?– Mozammil
Jan 20 at 14:34
What's the output of
count($scheduledMatches);
?– Mozammil
Jan 20 at 14:34
Its 0 if I add
has('homeClan', 'awayClan')
, 1 if I don't add the has('homeClan', 'awayClan')
.– Lee Nelson
Jan 20 at 14:35
Its 0 if I add
has('homeClan', 'awayClan')
, 1 if I don't add the has('homeClan', 'awayClan')
.– Lee Nelson
Jan 20 at 14:35
1
1
Could we also see your
AppClan
model? Particularly the relationships.– Mozammil
Jan 20 at 14:38
Could we also see your
AppClan
model? Particularly the relationships.– Mozammil
Jan 20 at 14:38
Updated the question with my Clan model.
– Lee Nelson
Jan 20 at 14:47
Updated the question with my Clan model.
– Lee Nelson
Jan 20 at 14:47
Hmm, tough to figure it out without seeing your table structure. Would you be able to post that as well?
– Mozammil
Jan 20 at 15:00
Hmm, tough to figure it out without seeing your table structure. Would you be able to post that as well?
– Mozammil
Jan 20 at 15:00
|
show 3 more comments
2 Answers
2
active
oldest
votes
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
add a comment |
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.
The data definitely exists, as its returning something. It's just the relationships that link it with the clan's table are returning null. I think this answer would be better as a comment also, but many thanks for your suggestion.
– Lee Nelson
Jan 20 at 14:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277452%2flaravel-belongsto-relationship-returning-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
answered Jan 20 at 14:43
Jagadesha NHJagadesha NH
1,03011022
1,03011022
add a comment |
add a comment |
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.
The data definitely exists, as its returning something. It's just the relationships that link it with the clan's table are returning null. I think this answer would be better as a comment also, but many thanks for your suggestion.
– Lee Nelson
Jan 20 at 14:48
add a comment |
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.
The data definitely exists, as its returning something. It's just the relationships that link it with the clan's table are returning null. I think this answer would be better as a comment also, but many thanks for your suggestion.
– Lee Nelson
Jan 20 at 14:48
add a comment |
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.
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.
edited Jan 20 at 18:01
Ahmed Nour Jamal El-Din
7181513
7181513
answered Jan 20 at 14:35
Konstantin SofKonstantin Sof
11
11
The data definitely exists, as its returning something. It's just the relationships that link it with the clan's table are returning null. I think this answer would be better as a comment also, but many thanks for your suggestion.
– Lee Nelson
Jan 20 at 14:48
add a comment |
The data definitely exists, as its returning something. It's just the relationships that link it with the clan's table are returning null. I think this answer would be better as a comment also, but many thanks for your suggestion.
– Lee Nelson
Jan 20 at 14:48
The data definitely exists, as its returning something. It's just the relationships that link it with the clan's table are returning null. I think this answer would be better as a comment also, but many thanks for your suggestion.
– Lee Nelson
Jan 20 at 14:48
The data definitely exists, as its returning something. It's just the relationships that link it with the clan's table are returning null. I think this answer would be better as a comment also, but many thanks for your suggestion.
– Lee Nelson
Jan 20 at 14:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54277452%2flaravel-belongsto-relationship-returning-null%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What's the output of
count($scheduledMatches);
?– Mozammil
Jan 20 at 14:34
Its 0 if I add
has('homeClan', 'awayClan')
, 1 if I don't add thehas('homeClan', 'awayClan')
.– Lee Nelson
Jan 20 at 14:35
1
Could we also see your
AppClan
model? Particularly the relationships.– Mozammil
Jan 20 at 14:38
Updated the question with my Clan model.
– Lee Nelson
Jan 20 at 14:47
Hmm, tough to figure it out without seeing your table structure. Would you be able to post that as well?
– Mozammil
Jan 20 at 15:00