Query on a column of string of an object where x key is a y value Laravel Eloquent

Viewed 21

I have a column called type that the value could be an object as a string, for example:

type = "{ 'a' : 'test1', 'b' : 'test2' }";

I want to get using Eloquent all rows where type.b = "test";

I can't access b in my where statement as it's not a column variable. I know I can use regex for it but I want to see if there is a better approach.

1 Answers

You should add "casts" in your Model like this

protected $casts = [
        'type' => 'array'
    ];

And after it you can access b like this

$model->type['b'];
Related