Laravel很多很多渴望加载总是返回空结果

时间:2013-10-22 04:24:50

标签: php mysql laravel eloquent eager-loading

我正在尝试获取所有Entry类,并在其属性上添加一些条件。一个关系,标签,应该是渴望加载。

模型看起来像这样

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');
    }
}

具有双重外键的表entry_tag,entry_id,tag_id,存在。

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

$testEntries = Entry::with(array('tags' => function($query)
{
    $query->where('tag_id', '=', '1');
}))->get();
但是,它什么也没有返回。 甚至使用下面的代码产生完全zilch。 的(2)

$testEntries = Entry::with('tags')->get();

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

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"]

和 的(4)

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));           
        }

编辑2: 我已经尝试使用其条目获取标签,如此

Tag::with('entries')->get();

但它(以及其他组合)每次都会返回零结果。我想也许我已经错过了我设置表格的方式。这是尝试(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}

1 个答案:

答案 0 :(得分:0)

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

相关问题