Support the ongoing development of Laravel.io →
Database Eloquent Architecture
Last updated 2 years ago.
0

Your GameTeam model should extend Model (Eloquent), not Pivot, if you want to work with it like an ordinary model. Calling it as a related is such thing.

Pivot models can't be instantiated without parameters required by the constructor, that are those you have in newPivot method. And this is what you do, when you call a relation.

Last updated 2 years ago.
0

Thanks to jarektkaczyk! But if I extend GameTeam by Eloquent I am not able to get the pivot between game and team anymore. For example:

foreach($game->teams as $team) { $team->pivot->id; //This causes an error because now GameTeam isnt a Pivot anymore!!! }

Could you provide me a minimalistic example where you access GameTeam from both sides, one time from $game and one time from $bet?? That would save my weekend!!!!!!

Big thanks!!!

Last updated 2 years ago.
0

Did you find the answer to your problem @peiphb02. I am facing exact same problem but could not find any solution yet. Looking for help.

Last updated 2 years ago.
0

Have the same problem, what jarektkaczyk posted is right. It cant be done with a Pivot, not if you want to call the relation from the Bet model. I'm going to fix it with a Eloquent model instead of a Pivot. I will post examples soon, if i succeed at least :)

Last updated 2 years ago.
0

It would be nice if someone can post sample code that solves this problem.

Last updated 2 years ago.
0

I ran into the same issue. Does anyone solved the problem?

Last updated 2 years ago.
0

Not sure if this is just a Laravel version issue but you must extend Illuminate\Database\Eloquent\Relations\Pivot, instead of the model class.

Example:

<?php namespace App\Pivots;

use Illuminate\Database\Eloquent\Relations\Pivot;

class MyCustomPivot extends Pivot {

...

Then on the model you are targeting:


public function newPivot(Model $parent, array $attributes, $table, $exists)
{
	return new MyCustomPivot($parent, $attributes, $table, $exists);
}

But I am on the 5.0 develop branch. Don't know enough about 4.1 or 4.2 to confirm that this is the issue.

Last updated 9 years ago.
0

Is there a solution for this?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

peiphb02 peiphb02 Joined 23 May 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.

© 2024 Laravel.io - All rights reserved.