Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

untymage's avatar

Disable CastAttribute when performing a query ?

I have a column which accepts a CastAttribute , How can i temporary disable casting when i perform a query ? I mean:

$post->comments()->pluck('level');

the raw property in my table have integer value but i casted it to string, How can tell laravel to ingore casting for level property in this query ?

0 likes
6 replies
martinbean's avatar

@untymage A cast will only do a query if you’re actually doing a query in the cast class (which you shouldn’t). A cast should just convert one value to a different representation.

tykus's avatar
tykus
Best Answer
Level 104

If you drop down to a Query Builder, you will get the raw attribute value:

$post->comments()->toBase()->pluck('level');
2 likes
untymage's avatar

i faced a problem which all models becomes a php stdClass if i use toBase :(

tykus's avatar

The solution I gave you was for the example you provided, i.e. using pluck. It was the simplest way to solve the problem you described, I never suggested it would make sense in the context of a Model instance.

What have you changed to to now (try to) get model instances instead?

untymage's avatar

I have another queries which uses groupBy so after this method i want use my model's methods but all of them become stdClass for example:

$post->comments()->toBase()->groupBy('level')->collapse()-> (cannot use my models here) because of stdclass
tykus's avatar

You have taken my solution to a different scenario, and tried to apply it elsewhere; why did you think a base Query Builder was needed in this case?

Please or to participate in this conversation.