0

I am trying to get all the ids with coma separated while doing eloquent relationship.

So here is my current queries

Divrank::where('division_id', 591)->with('meta')->orderBy('position', 'asc')->get()

Divrank table has a one to many relation with Divrankmeta model. So with meta I am trying to return

public function meta(){
    return $this->hasOne(Divrankmeta::class)->selectRaw('id, match_id,divrank_id, sum(won) as won, sum(loss) as loss, sum(draw) as draw, sum(points) as points, sum(matchePlayed) as matchePlayed, sum(totalSets) as totalSets, sum(totalGames) totalGames')
    ->groupBy('divrank_id');
}

So far this query works fine..

I get the result like this screenshot

enter image description here

Ok so in my Divrankmeta model, I have a column called winAgainst and it can have some ids and some left null. So with the meta relation I want to retrieve winAgainst ids with coma separated string inside meta object.

For better understanding, here is how Divrankmeta table looks like

enter image description here

How can I do this?

Thank you.

1
  • You didn't fetch winAgainst from Divrankmeta. Commented Oct 17, 2020 at 14:31

2 Answers 2

1

The relation you created is one-to-one not one-to-many. That's why you are getting a meta object of the first matched row instead of an array that contains all related meta records.

I never put the modification codes into the eloquent functions. Those codes seem belongs to somewhere else. From my perspective, using "resources" and modifying the data there is a better idea.

If you chose the do so:

// Divrank.php
public function metas()
{
   return $this->hasMany('App\Models\Divrankmeta');
}

// Divrankmeta.php
public function divrank()
{
    return $this->belongsTo('App\Models\Divrank');
}

// DivrankController
public function index()
{
  return DivrankResource::collection(Divrank::with("metas")->all());
}

Create a resource file.

php artisan make:resource DivrankResource

Now, you can modify your Divrank collection on the resource file before your controller returns it.

public function toArray($request)
{
   $metaIds = [];
   forEach($this->metas as $meta) {
     array_push($metaIds, $meta['id']);
   }
   $this['metaIds'] = $metaIds;
   return parent::toArray($request);
}

I'm not able to test this code. But it will probably work. If you don't want to use resources, you can create the same functionality in your controller as well. Bu we like to make controllers as short as possible.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your comment, it works my answer.. It has one to many relations but I need one data object so one to one relations is placed with a group by so it returns one record only. :)
0

Ok I think I solved it, These are the changes I did. Thanks

return $this->hasOne(Divrankmeta::class)
    // ->selectRaw('id, match_id,divrank_id, sum(won) as won, sum(loss) as loss, sum(draw) as draw, sum(points) as points, sum(matchePlayed) as matchePlayed,
    // sum(totalSets) as totalSets, sum(totalGames) totalGames')
    ->select(\DB::raw("id, match_id,divrank_id, sum(won) as won, sum(loss) as loss, sum(draw) as draw, sum(points) as points, sum(matchePlayed) as matchePlayed,
    sum(totalSets) as totalSets, sum(totalGames) totalGames, GROUP_CONCAT(winAgainst) as winAgainst"))->groupBy('divrank_id');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.