我现在主要在研究两个模型,Form
and Notification
,并且多对多关系已建立并适用于大多数 Eloquent 命令,除了whereHas
and has
。两者都只是返回一个空数组,[]
.
看来开发商遇到了麻烦让这个在过去发挥作用 https://github.com/jenssegers/laravel-mongodb/issues/131,但似乎已经解决了here https://github.com/jenssegers/laravel-mongodb/issues/182.
这是我迄今为止所拥有的以及我所尝试过的示例:
Form.php
class Form extends Eloquent {
protected $connection = 'mongodb';
public function notifications(){
return $this->belongsToMany('App\Api\Forms\Notification', null, 'form_ids', 'notification_ids');
}
}
通知.php
class Notification extends Eloquent {
protected $connection = 'mongodb';
public function forms()
{
return $this->belongsToMany('App\Api\Forms\Form', null, 'notification_ids', 'form_ids');
}
}
通知控制器.php
<?php
namespace App\Http\Controllers;
use App\Api\Forms\Notification;
use App\Api\Forms\Form;
class NotificationController extends Controller
{
public function getByFormTitle($form_title)
{
// This code retuns the relationship as expected.
// Any forms that are assigned to it are returned.
// $n = Notification::first();
// $n->forms()->get();
// This also returns the relationship correctly, same as before.
// $f = Form::first();
// $f->notifications()->get();
// Nearly identical to the Laravel docs. This returns an empty array, []
$notifications = Notification::whereHas('forms', function ($query) use ($form_title) {
$query->where('form_title', $form_title);
})->get();
return $notifications;
}
}
如果我使用,我会得到相同的结果Notification::has('form')->get()
.
所以我的问题是:
是否可以使用whereHas
and has
in Jenssegers\Mongodb Eloquent
?我是否必须使用与官方 Laravel 文档不同的语法,或者我是否必须为此进行原始 Mongo 查询?