memii said:
$locations = Location::with(['leagues.matches' => function($query) { $query->whereBetween('date', [Carbon::now()->subDays(7), Carbon::now()->addDays(7)])->with('team1', 'team2'); }])->get();
Try throwing a first() on that inner condition:
$locations = Location::with(['leagues.matches' => function($query) {
$query->whereBetween('date', [Carbon::now()->subDays(7), Carbon::now()->addDays(7)])->with('team1', 'team2')->first();
}])->get();
I've come across this issue in the past and - from memory - that was how I solved it. I'll see if I can find the code that caused me the issue to verify.
shabushabu said:
Really?
Yeah, i swear i never thought this could happen, i don't even know how it works this way !!
deringer said: Try throwing a first() on that inner condition:
$locations = Location::with(['leagues.matches' => function($query) { $query->whereBetween('date', [Carbon::now()->subDays(7), Carbon::now()->addDays(7)])->with('team1', 'team2')->first(); }])->get();
I've come across this issue in the past and - from memory - that was how I solved it. I'll see if I can find the code that caused me the issue to verify.
Thanks deringer, I've tried this, but it didn't work !!!
To whoever changed the title of this thread:
Please read carefully because it is a bug, it's not just my ignorance !!!
Would it be accurate to say that your database table which stores matches has a column named "team1"?
The really wasn't because of your question, but because of your initial title, making it sound as if there was a big security bug in Laravel, when all you needed to do was read the docs. And it's not a bug.
Basically, you'll need a one to many relationship between a match and teams. The fact that there should only ever be 2 teams is irrelevant, cause it's still more than 1.
The way it seems to be set up right now is that you have 2 columns (team1 and team2) in the matches database, with 2 one to one relationships set up. To me that seems inflexible.
And a get() will always give you a collection. A first() gets you a single result. Not within the sub-query, though. The one in the end. Note that you can then also use all the collection methods.
shabushabu - Like your username, but I'm pretty sure the issue the OP is having isn't with the relationship type.
In addition, chances are it isn't a bug. So the OP should pull his head in a bit and adjust his attitude. :)
If I'm not very much mistaken, the problem is that the relation functions on the match model (team1 and team2) have the same name as the relation fields in the database. In cases like this, almost everything works, except for fetching the related models/collections implicitly through dynamic properties, which return the IDs in the database fields rather than the related models.
The simple solution is to rename the fields in the database to team1_id and team2_id respectively. Then everything should just work.
Thank you all for the help and support. Yeah, @dfcowell is right, i found this solution almost an hour ago, but then i went for lunch and couldn't tell you guys..
Now, instead of renaming the database table columns i renamed the relationships to 'team_1' and 'team_2' and that works just fine.
Thanks again for your time. Memi
Be careful with naming relations using snake_case
, since doing so means you will be unable to use dynamic properties http://laravel.com/docs/eloquent#dynamic-properties without eager loading the relationship.
See below:
class Foo extends Eloquent {
public function snake_relation() { // any relation }
public function camelRelation() { ... }
}
// without eager loading
$foo = Foo::first();
$foo->snake_relation; // tries to call $foo->snakeRelation(), so returns null
$foo->camelRelation; // calls $foo->camelRelation and returns its result
// with eager loading, works as expected:
$foo = Foo::with('snake_relation')->first();
$foo->snake_relation; // returns the relation as expected, for it's been already loaded
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community