1

I'm trying to insert json into an existing json object returned by eloquent in Laravel.

$fixtures = Fixture::where('week', $week)->get();
foreach($fixtures as $key => $fixture){
        $fixtureinfos = FixtureInfo::where('fixture_id', $fixture[$key]['id'])->get();
        $fixture[$key]['fixtureinfos'] = $fixtureinfos;
 }

I am getting the error "Indirect modification of overloaded element of App\Fixture has no effect"

How should I be inserting '$fixtureinfos' into the existing '$fixture'?

Thanks in advance

Diego
  • 89
  • 1
  • 11
  • This may be related http://stackoverflow.com/questions/20053269/indirect-modification-of-overloaded-element-of-splfixedarray-has-no-effect. Even if it's not the same class, the workaround may help. –  Aug 14 '16 at 14:36
  • Terminus, you are right. It is related. I'll post the solution. Thanks for your help – Diego Aug 14 '16 at 15:04
  • I cannot see any [JSON](https://en.wikipedia.org/wiki/JSON) in your question. – axiac Aug 14 '16 at 17:25
  • It's what Eloquent returns into $fixtures and $fixtureinfos – Diego Aug 15 '16 at 05:15

2 Answers2

1

As mentioned by Terminus, this is related to this post.

Here is the solution to the case in this post.

$fixtures = Fixture::where('week', $week)->get();
foreach($fixtures as $key => $fixture){
    $fixtureinfos = FixtureInfo::where('fixture_id', $fixture[$key]['id'])->get();
    $arraytemp = $fixtures[$key];
    $arraytemp['fixtureinfos'] = $fixtureinfos;
    $fixtures[$key] = $arraytemp;
}
Community
  • 1
  • 1
Diego
  • 89
  • 1
  • 11
1

You should apply relation between those tables

In Fixture model

function fixtureinfo(){
    return $this->belongsTo(FixtrureInfo::class);
}

In FixtureInfo model

function fixture(){
    return $this->hasOne(Fixtrure::class);
}

Then in controller. You easily to use Eager Loading

$fixtures = Fixture::where('week', $week)->with('fixtureinfo')->get();

Then you can easily to access fixtureinfo like this

$fixtures[0]->fixtureinfo

Use a loop if you want

Elie Faës
  • 3,215
  • 1
  • 25
  • 41
KmasterYC
  • 2,294
  • 11
  • 19