标签云

微信群

扫码加入我们

WeChat QR Code

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 => "oliver@mywebsite.com"]]I am trying to save the above array, into my database table called email_rules: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?


The dump is after the validation. Changing it to $this->validate.. does not work

2019年04月25日30分54秒

Ah!I understand now.I think I see the problem.I'll edit my answer.

2019年04月25日30分54秒

The array is from Laravels default request().

2019年04月25日30分54秒

oliverbj sorry my bad english. maybe is there a better way than nested foreach, but you can try like in the edit of my answer

2019年04月25日30分54秒

Pablo - your English is fine.And your answer is good.I think we're both pointing to the same error.

2019年04月25日30分54秒

Watercayman Yes! I think the answers point to the same and complement each other. Also, you argue the cause of the issue.

2019年04月25日30分54秒