Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Eloquent with function BelongsToMany not working #2284

Unanswered
blpraveen asked this question in Q&A
Discussion options

  • Laravel-mongodb Version: Latest
  • PHP Version:7.4
  • Database Driver & Version:
    Monogdb

Description:

 $this->getPermissionClass()->with('roles')->get()

My Model Permission is having this function

 public function roles(): BelongsToMany
 {
 return $this->belongsToMany(
 config('permission.models.role'),
 config('permission.collection_names.role_has_permissions'),
 'permission_id',
 'role_id'
 );
 }

it does not work
relation returns empty

namespace Illuminate\Database\Eloquent\Relations\BelongsToMany;

public function get($columns = ['*'])
{
 // First we'll add the proper select columns onto the query so it is run with
 // the proper columns. Then, we will get the results and hydrate our pivot
 // models with the result of those columns as a separate model relation.
 $builder = $this->query->applyScopes();
 $columns = $builder->getQuery()->columns ? [] : $columns;
 $models = $builder->addSelect(
 $this->shouldSelect($columns)
 )->getModels();
 $this->hydratePivotRelation($models);
 // If we actually found models we will also eager load any relationships that
 // have been specified as needing to be eager loaded. This will solve the
 // n + 1 query problem for the developer and also increase performance.
 if (count($models) > 0) {
 $models = $builder->eagerLoadRelations($models);
 }
 
 return $this->related->newCollection($models);
}

This function should return collection model of related but returns empty

You must be logged in to vote

Replies: 3 comments

Comment options

I traced the flow
And found that sql
is have ? in the query

select "roles".*, "role_has_permissions"."permission_id" as "pivot_permission_id",
"role_has_permissions"."role_id" as "pivot_role_id" from "roles" inner join "role_has_permissions" on "roles"."_id" =
"role_id" where "permission_id" in (?, ?, ?)

namespace Illuminate\Database\Eloquent\Relations\Relation;
/**
 * Get the name of the "where in" method for eager loading.
 *
 * @param \Illuminate\Database\Eloquent\Model $model
 * @param string $key
 * @return string
 */
 protected function whereInMethod(Model $model, $key)
 {
 return $model->getKeyName() === last(explode('.', $key))
 && in_array($model->getKeyType(), ['int', 'integer'])
 ? 'whereIntegerInRaw'
 : 'whereIn';
 }

this is returning whereIntegerInRaw if integer
to Mongodb the methid is whereIn

This is because

namespace Illuminate\Database\Query\Grammars\Grammar
 /**
 * Compile a "where in" clause.
 *
 * @param \Illuminate\Database\Query\Builder $query
 * @param array $where
 * @return string
 */
 protected function whereIn(Builder $query, $where)
 {
 if (! empty($where['values'])) {
 return $this->wrap($where['column']).' in ('.$this->parameterize($where['values']).')';
 }
 return '0 = 1';
 }
```

namespace Illuminate\Database\Grammar
/**
* Create query parameter place-holders for an array.
*
* @param array $values
* @return string
*/
public function parameterize(array $values)
{
return implode(', ', array_map([$this, 'parameter'], $values));
}

/**
 * Get the appropriate query parameter place-holder for a value.
 *
 * @param mixed $value
 * @return string
 */
public function parameter($value)
{
 return $this->isExpression($value) ? $this->getValue($value) : '?';
}

Please help me to solve this?
 /**
 * Get the name of the "where in" method for eager loading.
 * @param \Illuminate\Database\Eloquent\Model $model
 * @param string $key
 * @return string
 */
 protected function whereInMethod(EloquentModel $model, $key)
 {
 return 'whereIn';
 }
You must be logged in to vote
0 replies
Comment options

Jenssegers\Mongodb\Relations\BelongsToMany whereInMethod is not called


Get Relation return an instance of Illuminate\Database\Eloquent\Relations\BelongsToMany
 Illuminate\Database\Eloquent\Builder
/**
 * Get the relation instance for the given relation name.
 *
 * @param string $name
 * @return \Illuminate\Database\Eloquent\Relations\Relation
 */
 public function getRelation($name)
 {
 // We want to run a relationship query without any constrains so that we will
 // not have to remove these where clauses manually which gets really hacky
 // and error prone. We don't want constraints because we add eager ones.
 $relation = Relation::noConstraints(function () use ($name) {
 try {
 return $this->getModel()->newInstance()->$name();
 } catch (BadMethodCallException $e) {
 throw RelationNotFoundException::make($this->getModel(), $name);
 }
 });
 $nested = $this->relationsNestedUnder($name);
 // If there are nested relationships set on the query, we will put those onto
 // the query instances so that they can be handled after this relationship
 // is loaded. In this way they will all trickle down as they are loaded.
 if (count($nested) > 0) {
 $relation->getQuery()->with($nested);
 }
 return $relation;
 }
You must be logged in to vote
0 replies
Comment options

this is the final query i am getting
select "permissions".*, "role_has_permissions"."role_id" as "pivot_role_id", "role_has_permissions"."permission_id" as
"pivot_permission_id" from "permissions" inner join "role_has_permissions" on "permissions"."_id" = "permission_id"
where "role_id" = ?

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant
Converted from issue

This discussion was converted from issue #2283 on July 03, 2021 11:56.

AltStyle によって変換されたページ (->オリジナル) /