简体   繁体   English

Laravel MongoDB hasMany 关系不起作用

[英]Laravel MongoDB hasMany relationship is not working

I'm try to create a relationship between albums and photos (an Album has many photos).我正在尝试在相册和照片之间建立关系(一个相册有很多照片)。 Below is my controller and what my models look like.下面是我的 controller 和我的模型的样子。 Interesting enough, the reverse relationship photo->album (belongsTo) works fine.有趣的是,反向关系 photo->album (belongsTo) 工作正常。 but the album->photos returns an empty collection但是专辑->照片返回一个空集合

## The hasMany relationship does NOT work... I get an empty collection
<?php
class AlbumController extends BaseController
{
    public function show(Request $request, $album_id)
    {
        $album = Album::find($album_id);
        dd($album->photos);
    }
}

## Results:
# Collection {#418
#  items: []
# }


## The belgonsTo relationship works
<?php
class PhotoController extends BaseController
{
    public function show(Request $request, $photo_id)
    {
        $photo = Photo::find($photo_id);
        dd($photo->album);
    }
}

<?php

namespace App;

use DB;
use Jenssegers\Mongodb\Eloquent\SoftDeletes;
use Moloquent;

class Album extends Moloquent
{
    use RecordActivity, SoftDeletes;

    protected $connection = 'mongodb';
    protected $table = 'albums';
    protected $collection = 'albums';
    protected $primaryKey = "_id";
    protected $dates = ['deleted_at'];
    protected $fillable = ['user_id','name','is_private'];

    public function photos()
    {
        // Neither seems to work
        //return $this->embedsMany('Photo');
        return $this->hasMany('App\Photo');
    }
}

<?php

namespace App;

use DB;
use Jenssegers\Mongodb\Eloquent\SoftDeletes;
use Moloquent;

class Photo extends Moloquent
{
    use RecordActivity, SoftDeletes;

    protected $connection = 'mongodb';
    protected $table = 'photos';
    protected $collection = 'photos';
    protected $primaryKey = "_id";
    protected $dates = ['deleted_at'];
    protected $fillable = ['album_id', 'user_id', 'name', 'folder', 'is_private', 'caption'];
    protected $hidden = [];

    // user and album belongsTo works 
    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function album()
    {
        return $this->belongsTo('App\Album');
    }
}

The issue had to do with the fact that my IDs were ObjectID and it seems to be an issue with Jessengers Laravel MongoDB Drivers... we have actually decided to move back to MariaDB to fully utilize Eloquent/Relationships这个问题与我的 ID 是 ObjectID 的事实有关,它似乎是 Jessengers Laravel MongoDB 驱动程序的问题......我们实际上已经决定回到 Z7F9733E208088B1CE6DF3D4BE1765396 充分利用 Elo

I did the same thing as yours and i found that nothing wrong with Mongodb.我做了和你一样的事情,我发现 Mongodb 没有问题。 Because Mongodb defined the "_id" as primary key and that's the reason it couldn't get the correct relationship: belongsTo and hasMany.因为 Mongodb 将“_id”定义为主键,这就是它无法获得正确关系的原因:belongsTo 和 hasMany。 So i did a small change by declared the $primaryKey = "id" on the top of parent Model and it worked fine所以我做了一个小改动,在父 Model 的顶部声明了 $primaryKey = "id" 并且效果很好

this worked for me.这对我有用。

 /**
 * @return HasMany
 */
public function tasks(): HasMany
{
    return $this->hasMany(ProjectTask::class, 'project_id', 'idAsString');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM