Potti
1 week ago

using groupBy() on table relations

Posted 1 week ago by Potti

I have a little problem about grouping belongsTo() tables...

Place Model

protected $table = "places";
protected $guarded = [];

public $with = ["plans"];

public function plans()
{
    return $this->hasMany("App\Models\Plan");
}

Plan Model

protected $guarded = [];

public function place()
{
    return $this->belongsTo("App\Models\Place");
}

In controller when I return json data I can see the relation.

$place = Place::with(["plans"])->get();

return $place;

The result is fine with this. check the image: https://imgur.com/a/0CHXPhQ

But, when I try to use groupBy() on place_name column. It doesn't group their plans in one place...

Place::with(["plans"])->groupBy("place_name")->get(); 

Results: https://imgur.com/a/qPxMU42 shows. plans doesn't grouping. It removes second place_id's plans...

Please sign in or create an account to participate in this conversation.