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.
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!!!
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.
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 :)
It would be nice if someone can post sample code that solves this problem.
I ran into the same issue. Does anyone solved the problem?
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community