jeudi 18 octobre 2018

With spatie/laravel-tags plugin get related items

using spatie/laravel-tags plugin https://docs.spatie.be/laravel-tags/v2/basic-usage/using-tags in my Laravel 5.7 app My Vote model app/Vote.php has tags :

<?php

namespace App;

use DB;
use App\MyAppModel;
...
use Spatie\Tags\Tag as SpatieTag;
use Spatie\Tags\HasTags;

class Vote extends MyAppModel
{
    use HasTags;

    protected $table = 'votes';
    protected $primaryKey = 'id';

and I try by found Tag get all related Votes, which has this tag, like:

    $activeTag = Tag::containingSlug($tag_slug)->first();
    $tagRelatedVotes= Vote::withAnyTags( [$activeTag->slug], 'votesTagType' )->get();

But tagRelatedVotes is empty and looking at sql trace I see next:

   SELECT * 
    FROM `tags` 
    WHERE LOWER(JSON_EXTRACT(slug, "$.en")) like '"%animals%"' limit 1 


   SELECT * 
    FROM `tags` 
    WHERE `name`->'$."en"' = '{"en": "animals"}'     AND `type` = 'votesTagType' limit 1 


   SELECT * 
    FROM `votes` 
    WHERE 
    EXISTS (  SELECT * 
    FROM `tags` 
    INNER JOIN `taggables` on `tags`.`id` = `taggables`.`tag_id` 
    WHERE `votes`.`id` = `taggables`.`taggable_id`     AND `taggables`.`taggable_type` = 'App\Vote'     AND `id` in ('')) 

The 1st statement find the row, but the second statement finds nothing and that is strange why name field is used in request ? So the 3rd statement is invalid. Which is the valid way?

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire