Php Laravel多对多即时加载总是返回空结果

Php Laravel多对多即时加载总是返回空结果,php,mysql,laravel,eloquent,eager-loading,Php,Mysql,Laravel,Eloquent,Eager Loading,我正在尝试获取所有类的条目,并对其属性设置一些条件。应该加载一个关系标记 模型看起来是这样的 class Tag extends Eloquent { protected $table = 'tags'; protected $guarded = array(); public function entries() { return $this->belongsToMany('Entry'); } } class Entry ext

我正在尝试获取所有类的条目,并对其属性设置一些条件。应该加载一个关系标记

模型看起来是这样的

class Tag extends Eloquent {
    protected $table = 'tags';
    protected $guarded = array();
    public function entries()
    {
        return $this->belongsToMany('Entry');
    }
}

class Entry extends Eloquent {
    protected $table = 'entries';
    protected $guarded = array();
    public function tags()
    {
        return $this->belongsToMany('Tag');
    }

    public function user()
    {
        return $this->belongsTo('User');
    }

    public function votes()
    {
        return $this->hasMany('Votes');
    }
}
存在带有双外键的表条目\标记、条目\ id、标记\ id

我正在尝试使用此代码。 (1)

然而,它什么也不返回。 即使使用下面的代码也会产生完全无效的结果。 (2)

检查数据库日志,我可以看到查询是正常的。他们屈服了 (3)

(4)

在手动执行查询时,这两种方法都可以工作(并找到结果)

我错过了什么?我已经搔头好几个小时了

编辑:

我尝试过显示结果,但没有任何运气,如下所示

Log::debug('testing fetching entries:: ' . json_encode($testEntries));

编辑2: 我尝试过用标签的条目获取标签,就像这样

Tag::with('entries')->get();
但它(连同其他组合)每次都返回零结果。我在想,也许我在摆桌子的方式上遗漏了一些基本的东西。以下是trument(2)的完整sql输出,以防有所帮助

{"query":"select * from `entries`","bindings":[],"time":0.33},{"query":"select `tags`.*, `entry_tag`.`entry_id` as `pivot_entry_id`, `entry_tag`.`tag_id` as `pivot_tag_id` from `tags` inner join `entry_tag` on `tags`.`id` = `entry_tag`.`tag_id` where `entry_tag`.`entry_id` in (?, ?, ?, ?, ?, ?, ?, ?)","bindings":["1","2","3","4","5","6","7","8"],"time":0.74}

如果您使用软删除特征,请检查
deleted_at
是否具有默认值
NULL

不确定这是否有帮助,但请尝试这样定义您的belongToMany()关系:
return$this->belongToMany('Entry'、'Entry_tag'、'Entry_id'、'tag_id')和反之亦然。您如何尝试显示结果?可能是某个地方有输入错误。@GladToHelp,没有变化。因为它当前创建了成功的sql查询(查看数据库日志),所以这没有什么区别。@user1669496我将信息添加到了问题中。@C-A当您尝试在不进行筛选或快速加载的情况下获取单个条目时,它是否起作用?类似于
$testEntries=Entry::find(1)
select `tags`.*, `entry_tag`.`entry_id` as `pivot_entry_id`, `entry_tag`.`tag_id` as `pivot_tag_id` from `tags` inner join `entry_tag` on `tags`.`id` = `entry_tag`.`tag_id` where `entry_tag`.`entry_id` in (?, ?, ?, ?, ?, ?, ?, ?) and `tag_id` = ?","bindings":["1","2","3","4","5","6","7","8","1"]
Log::debug('testing fetching entries:: ' . json_encode($testEntries));
foreach(Entry::with('tags')->get() as $entry)
        {
            Log::debug('test1!! ' . json_encode($entry));           
        }
Tag::with('entries')->get();
{"query":"select * from `entries`","bindings":[],"time":0.33},{"query":"select `tags`.*, `entry_tag`.`entry_id` as `pivot_entry_id`, `entry_tag`.`tag_id` as `pivot_tag_id` from `tags` inner join `entry_tag` on `tags`.`id` = `entry_tag`.`tag_id` where `entry_tag`.`entry_id` in (?, ?, ?, ?, ?, ?, ?, ?)","bindings":["1","2","3","4","5","6","7","8"],"time":0.74}