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

None of the Relations work #2119

Answered by divine
silicahd asked this question in Q&A
Discussion options

  • Laravel-mongodb Version: dev-develop
  • PHP Version: 7
  • Database Driver & Version: atlas

Description:

Ok I have been working on this for three days with no results, I have to assume at this point that there might be bug that is causing this.
I am setting up an extremely simple relation hasOne

// Categories
{
	_id: ObjectId('5c8445700000000000000000'),
	name: 'Foo',
	description: 'Test foo'
}
// Posts
{
	_id: ObjectId('5c8445700000000000000001'),
	name: 'Bar',
	text: 'Test foo',
	category_id: ObjectId('5c8445700000000000000000')
}

The Controller Code

 protected $collection = 'category';
 protected $primaryKey = '_id';
 protected $guarded = [];
 protected $casts = [
 'category_id' => 'string',
 ];
public function category()
{
 return $this->hasOne(Categories::class, '_id', 'category_id');
}

Blade

 $category = Categories::where('name', 'Foo')->first();
 dd($category->parent());

No matter what I did or tried the result relation is always empty.

Anybody has any ideas?

You must be logged in to vote

Hello,

Why are you using dev-develop version and not the latest stable ones?

Thanks!

Replies: 1 comment 3 replies

Comment options

Hello,

Why are you using dev-develop version and not the latest stable ones?

Thanks!

You must be logged in to vote
3 replies
Comment options

Please try reading relations documentation again.

You need to understand parent children relation and use correct function to get children instead of getting parent from parent.

Comment options

Not sure why it was dev but I just did an composer update and installed 3.7 but still not results.

I am confused all I am looking to do is to get all the posts of a category. The issues with the documentation is that it does not show how the collection needs to be set up. It notes that hasOne should work maybe I am missing something.

Comment options

Never mind I had to restart the server I realized that I had npm run watch still on. I guess the dev version might have been it. Thank you

Answer selected by silicahd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #2119 on October 13, 2020 00:57.

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