i have laravel 8 project with jenssegers (Mongodb Database) but my hasMany relationship not work between Product & Category Model my Product mongo object :
{
"_id" : ObjectId("5dcaafb8eaec3701a2774d29")
"category_ids" : [
ObjectId("5dcaacbfeaec37018b508a39"),
ObjectId("5dcaacbfeaec37018b508a5d")
]}
and my Category mongo object :
{
"_id" : ObjectId("5dcaacbfeaec37018b508a39"),
"title" : "category test",
"slug" : "wordpress-plugins"
}
my Product model code :
public function categories() {
return $this->hasMany(Category::class, 'category_ids', '_id');
}
but below code return null :
$product = Product::with('categories')->where('_id', $id)->first();
dd($product->categories);
please help me , thanks :)
You need to use
$this->belongsToMany
relation in both the modals.