php - Laravel Validation - Ensure Value Does Not Exist in Json Column

one text

I have a JSON field in my table, and It stores an array of json objects:

[
{
    "data": "123", 
    "name": "123"
}, 
{
    "data": "TEST", 
    "name": "456"
}, 
{
    "data": "123", 
    "name": "789"
}
]

I need to validate that when a user adds or edits a field that the name has not already been taken, and is distinct for that product.

I am able to do this in the controller like so:

public function store(Product $product) {
    if ($product->data_fields) {
        if (array_search($request->input('name'), array_column($product->data_fields, 'name')) !== false) {
            return response()->json([
                'success' => false,
                'data' => 'Data name already exists',
            ]);
        } else {
            $product->data_fields = array_merge($product->data_fields, [$request->all()]);
        }
    } else {
        $product->data_fields = [$request->all()];
    }

    $product->save();
}

But I would like to move it to a Form Request to keep the controller clean.

EDIT:

I tried the following validation:

public function rules()
{
    $rules = [
        "data" => 'required',
    ];

    $name = $this->name;

    $rules["name"] = [
        'required',
        'string',
        'max:255',
        Rule::exists('product')->where(function ($query) use ($name) {
            return $query->whereNot(DB::raw('JSON_EXTRACT(data_fields->name, "$.name)'), $name);
        })
    ];

    return $rules;
}

But I get the following error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'name, \"$.name))' at line 1 (SQL: select count(*) as aggregate from `product` where `name` = Name 1 and (`not` = JSON_EXTRACT(data_fields->name, \"$.name)))

Source