-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Relation hasMany will not work #1902
Comments
Hi bro, i also faced the same problem, then i saw this issue: #1383 |
@inkvc17, what is the version laravel-mongodb, php driver and mongo? |
@Smolevich this is related to this PR #1523 |
Hi @Smolevich , I use mongodb/mongodb 1.5.1 MongoDB Driver 1.6.1 Hope you can help |
try this: replace $instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey to $instance->newQuery(), $this, $foreignKey, $localKey such as /**
* Define a one-to-many relationship.
*
* @param string $related
* @param string $foreignKey
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function hasManyMongo($related, $foreignKey = null, $localKey = null)
{
$instance = $this->newRelatedInstance($related);
$foreignKey = $foreignKey ?: $this->getForeignKey();
$localKey = $localKey ?: $this->getKeyName();
return $this->newHasMany(
$instance->newQuery(), $this, $foreignKey, $localKey
);
} in my project's case : public function items(){
// return $this->hasMany(KjzxCheckTaskItem::class,'task_id','id');
$instance = $this->newRelatedInstance(KjzxCheckTaskItem::class);
return $this->newHasMany(
$instance->newQuery(), $this, 'task_id', 'id'
);
} |
I figure it out myself. In this repository, I have no idea how to resolve this in a creative way. Hopefully, this may help someone who is trying to figure out what's going on. [1] |
At the one side model: public function getIdAttribute($value = null)
{
return $value;
} A bit tricky, but works. I don't know if this brokes something but my tests are passing. |
@juanmanavarro worked like a charm with me, Thanks |
@juanmanavarro thanks, its worked for me. |
I know this is an old thread but, this works for hasMany. On my Laravel nova table, on the other hand, the id is missing and this leads to a 404 when I click on any record on the table. |
Hi,
I am a MongoDB rookie and need your help.
I have 2 simple models:
Model merchant
Controller
collection merchants
collection comments
If I only want to query the merchant, I don't have any problems, but if I want to use hasMany (relation to comments) it will not work
Illuminate\Database\Eloquent\Collection {#371 ▼
#items: []
}
Hope you can help me - many thanks
The text was updated successfully, but these errors were encountered: