Home >Backend Development >PHP Tutorial >Laravel 5.1 form submission data (including array) insert database error solution

Laravel 5.1 form submission data (including array) insert database error solution

WBOY
WBOYOriginal
2016-07-23 08:54:431372browse

When there is a need to submit an array to the database in the form, such as adding some customer service QQ information, Laravel will throw an error that the input field cannot be an array, and the solution is also very simple.

Adding multiple QQ customer service requires the use of arrays

Laravel 5.1 form submission data (including array) insert database error solution

Error thrown

  1. ErrorException in helpers.php line 671: preg_replace(): Parameter mismatch, pattern is a string while replacement is an array
Copy code

Solution:

In the model, "block" the fields that need to be submitted, use Input alone to receive and serialize them in the controller, and then save them.

Model example:

  1. class Shop extends Model
  2. {
  3. public $timestamps=false;
  4. protected $guarded = ['qq','submit'];
  5. }
Copy code

Controller example

  1. public function store(CheckNameRequest $request)
  2. {
  3. $shop = Shop::create(Input::get());
  4. $shop->qq = serialize(Input::get('qq' ));
  5. $shop->save();
  6. }
Copy the code

Similarly, just deserialize it when displaying.

Original address: http://note.mango.im/article/24

Solution, Laravel


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn