1

I have complex query builder to get all image from images relation with images_tags table

$tags = [1, 2, 3];
$items = ImagesTable::with(['images_tags' => function ($query) use ($tags) {
   $tags = $query->select('image_id', ImageTag::raw('count(tag_id) as total'))
          ->whereIn('tag_id', $tags)
          ->groupBy('image_id')
          ->having(['total', 3])
          ->get();
      return $tags;
    }])->get();

And two table

(images)-(images_tags)  1-n
images (
  id int  
  title varchar(255) 
)
images_tags (
  id int  
  image_id,  
  tag_id int 
  constraints fk foreign key('image_id') on images('id')
)

But i get error

ErrorException in Grammar.php line 58:
strtolower() expects parameter 1 to be string, object given

And i found this line code cause error

ImageTag::raw('count(tag_id) as total')

And i don't know why is this?

3
  • 1
    Can you try using \DB instead of ImageTag. Or Try using \ before your ImageTag like \ImageTag. Try like these \ImageTag::raw('count(tag_id) as total') OR \DB::raw('count(tag_id) as total') Commented May 14, 2017 at 8:12
  • Yeah. You're true, @manian ! Thank you so much :) Commented May 14, 2017 at 9:48
  • I am glad that it worked. Happy coding :) Commented May 14, 2017 at 15:22

1 Answer 1

1

[SOLVED] This question is solved!
I replaced ImageTag with \DB and it works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.