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

4
  • This may be related stackoverflow.com/questions/20053269/…. Even if it's not the same class, the workaround may help. Commented Aug 14, 2016 at 14:36
  • Terminus, you are right. It is related. I'll post the solution. Thanks for your help Commented Aug 14, 2016 at 15:04
  • I cannot see any JSON in your question. Commented Aug 14, 2016 at 17:25
  • It's what Eloquent returns into $fixtures and $fixtureinfos Commented Aug 15, 2016 at 5:15

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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

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.