0

I am trying to fetch all classes of Entry with some condition on it's attributes. One relationship, tags, should be eager loaded.

The models looks like so

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

Table entry_tag with dual foreign keys, entry_id,tag_id, exists.

I am trying to use this code. (1)

$testEntries = Entry::with(array('tags' => function($query)
{
    $query->where('tag_id', '=', '1');
}))->get();

however, it returns nothing. even using the code below yields completely zilch. (2)

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

Inspecting the DB log, I can see that the queries are OK. They yield (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"]

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

which both work (and finds results) when executing the queries manually.

What am I missing? I've scratched my head a few hours now!

EDIT:

I've tried displaying the results, without any luck, like below

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

and

foreach(Entry::with('tags')->get() as $entry)
        {
            Log::debug('test1!! ' . json_encode($entry));           
        }

EDIT 2: I have tried fetching Tags with their entries, like so

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

but it (along with other combinations) returns zero results every time. I am thinking maybe I have missed something fundamental in the way I have set up tables. Here is the complete sql output for attempt (2), in case it helps.

{"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}
C-A
  • 699
  • 1
  • 6
  • 11
  • Not sure if this will help, but try to define your BelongsToMany() relationships like this: `return $this->belongsToMany('Entry', 'entry_tag', 'entry_id', 'tag_id');` and vice versa in the Entry table – Glad To Help Oct 22 '13 at 09:51
  • How are you attempting to display the results? Maybe there is a typo there somewhere. – user1669496 Oct 22 '13 at 14:54
  • @GladToHelp, no change. Since it currently creates the successful sql queries (looking at the DB log) it makes sense that it's no difference. – C-A Oct 22 '13 at 15:36
  • @user1669496 I added the information to the question. – C-A Oct 22 '13 at 15:37
  • @C-A Does it work when you try to fetch a single entry without filtering or eager loading? Something like `$testEntries = Entry::find(1);` ? – Glad To Help Oct 22 '13 at 15:40
  • @GladToHelp Yep works, `Entry::find(X);` and `Entry::all();` both gives back expected results. – C-A Oct 22 '13 at 17:29
  • @C-A have you managed to resolve this by now? – Glad To Help Oct 28 '13 at 15:24
  • @GladToHelp no unfortunately not, I am using a raw query instead. I've chugged it down to being a typo error somewhere. Next project I'll set this up immediately and try to get it working, to see if it's my local machine or not. – C-A Oct 30 '13 at 07:35

1 Answers1

0

If you use soft delete trait, check deleted_at has default value NULL.

Arthur Shlain
  • 961
  • 10
  • 23