Accessing JSON in TEXT columns

Posted 4 months ago by Bartude

I have a TEXT field in the database that has JSON. I'm having two problems.

The first is that when I try to access that field, even with casting that field in the model, it's not returning as proper JSON.

protected $casts = [
    'b_bom_json' => 'array',

To fix this I had to set an accessor, but even then it's weird cause the value field comes weirdly formatted that I had to decode it twice to return a proper json.

public function getBBomJsonAttribute($value){
        return json_decode(json_decode($value));

The second is that I've seen in quite some places, that with Eloquent I can access the json values directly in the where conditions, but it's throwing an error because it's not identifying the arrow.

public function boms() {
        return \App\BOM::where('b_bom_json->operator_id', $this->u_id)->get();

Is the second problem even feasible?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.