cartalyst / tags

A Tagging package that easily allows you to add tags to your Eloquent models.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

allTags() does not return anything

tjb328 opened this issue · comments

When using allTags in Laravel for a model, there are no results returned.
ex Model::allTags()

A quick fix for this was for me to alter TaggableTrait allTags() with this:

return DB::table('tags')->where('namespace', $instance->getEntityClassName())->where('count','>','0')->get();

What Laravel version?

Can't reproduce on Laravel v5.1.21.

Laravel 5.1.21
tags: 2.0.2

@rutger1413 I can't really reproduce any issue so i assume you're doing some wrong on your end, as you can see below, both snippets returns the expected response, considering i only have one product on the database.

Either you changed your models namespaces or you have something else going on your end that you'll need to debug.

Using allTags method
var_dump(App\Product::allTags()->lists('name'));
Response
object(Illuminate\Database\Eloquent\Collection)[140]
  protected 'items' => 
    array (size=3)
      0 => string 'foo' (length=3)
      1 => string 'bar' (length=3)
      2 => string 'baz' (length=3)
Using a model entity
var_dump(App\Product::find(1)->tags->lists('name'));
Response
object(Illuminate\Database\Eloquent\Collection)[139]
  protected 'items' => 
    array (size=3)
      0 => string 'foo' (length=3)
      1 => string 'bar' (length=3)
      2 => string 'baz' (length=3)

Ok I see what the issue is.

Model::allTags()

returns a QueryBuilder object.

Model::allTags()->get()

Returns the collection which is what I would have expected. The documentation isn't clear about this. I didn't var dump the result which is something I should have done.

By doing ->lists('name') the QueryBuilder object is now a collection. It would be good to point this out or to change allTags() to include the ->get() like so:

    /**
     * {@inheritDoc}
     */
    public static function allTags()
    {
        $instance = new static;

        //return DB::table('tags')->where('namespace', $instance->getEntityClassName())->where('count','>','0')->get();
        return $instance->createTagsModel()->whereNamespace(
            $instance->getEntityClassName()
        )->get();
    }

You can submit a PR to the documentation if you want.