guys in laravel relation, how can i change Model primary key on the fly? like
/** table structures
* model1
* -- id (primary_key)
* -- uid (i just used this as an identifier to another model)
* model2
* -- id (primary_key)
* -- uid (i just used this as an identifier to another model)
* model1_model2
* -- id
* -- model1_id (uid of model1)
* -- model2_id (uid of model2)
**/
// in Model1.php
public function relation()
{
return $this->belongsToMany(Model2::class, 'model1_model2', 'mode1_id', 'mode2_id');
}
the question is. how can i change the Model1 primary key when calling the Model1::relation()
??..
Sorry for pushing this old post. But recently I had the same problem and found just this post.
So I did some research and found out that since Version 5.5 Laravels belongsToMany
method supports an $parentKey
and $relatedKey
variable as fifth and sixth parameter. https://laravel.com/api/5.5/Illuminate/Database/Eloquent/Concerns/HasRelationships.html#method_belongsToMany