Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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.

0

shabushabu said:

Really?

Yeah, i swear i never thought this could happen, i don't even know how it works this way !!

0

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 !!!

0

To whoever changed the title of this thread:

Please read carefully because it is a bug, it's not just my ignorance !!!

0

Would it be accurate to say that your database table which stores matches has a column named "team1"?

0

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.

http://laravel.com/docs/4.2/eloquent#one-to-many

0

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.

Last updated 9 years ago.
0

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

0

@memii

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
Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

memii memii Joined 29 Dec 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.