Saving array to DB - Grammar::parameterize() error

Posted 4 months ago by oliverbusk

I have an array, which looks like this:

array:3 [▼
  "field" => array:2 [▼
    0 => "fromName"
    1 => "from"
  ]
  "operator" => array:2 [▼
    0 => "="
    1 => "="
  ]
  "value" => array:2 [▼
    0 => "Oliver"
    1 => "[email protected]"
  ]
]

I am trying to save the above array, into my database table called email_rules:

My database table

Below is my code.

StreamEmailRulesController.php:

public function store(Stream $stream)
{
    //Validate the request.
    //Validate the request.
    $attributes = request()->validate([
        'field' => 'required|array|min:1',
        'field.*' => [
            'required', 'string',
            Rule::in(['fromName', 'from']),
        ],
        'operator' => 'required|array|min:1',
        'operator.*' => [
            'required', 'string',
            Rule::in(['=', '!=', 'matches']),
        ],
        'value' => 'required|array|min:1',
        'value.*' => 'required|string|min:3|max:255',
    ]);
   
    //Add the document to the database.
    $stream->addRules($attributes);

    //Return back.
    return redirect()->back();
}

Now the $stream->addRules() function is responsible for saving the data to the database.

Stream.php:

/**
* A stream can have many rules.
*/
public function rules()
{
    return $this->hasMany(EmailRule::class);
}


/**
 * Add Email Rules(s) to the stream
 *
 * @return Illuminate\Database\Eloquent\Model
 */
public function addRules(array $attributes)
{
    return $this->rules()->create($attributes);
}

Now, above does not work. I get below error:

Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, int given, 

What am I doing wrong?

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