Support the ongoing development of Laravel.io →
Database Eloquent

Hi, I have two models :

Sale.php <?php

class Sale extends Eloquent {
  public function groups() {
    return $this->hasMany('Group');
  }

Group.php

class Group extends Eloquent{
    public function categories()	{
        
	    return $this->hasMany('Categorie');
    }

    public function items() {
	    return $this->hasMany('Item');
    }

    public function sale() {
	    return $this->belongsTo('Sale');
    }

}

the two tables sales and group are created in database, and there is a column sale_id in groups.

But when i use $sale->groups(), there is no value return (it's empty). The object $sale is correct, it has all its attributes set.

Someone have an idea?

Last updated 2 years ago.
0
dd($sale->groups->toArray());

Does that result in an empty array too?

Last updated 2 years ago.
0

Yes, i have the good result. But i want to get all the objects "group", and with this code, i get only the attributes into an array...

Last updated 2 years ago.
0

Remove ->toArray() then.
The difference is basically ->groups() and ->groups. One is the collection/model, the other is the data. I think it's the data you want. Unfiltered, so no ->toJson() or ->toArray()

Last updated 2 years ago.
0

yes it works !

Thank you XoneFobic!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

soclosed soclosed Joined 5 Feb 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.

© 2025 Laravel.io - All rights reserved.