1

my problem here in my code is I cannot store or insert array values in Database MySQL, here is my controller code please help me. thank you. My Code in Controller has no array or any so.. I dont have any clue how to turn it into array.

Controller

    public function store(Request $request)
{
    $this->validate($request,[
        'city' => 'required'

    ]); 


   $citi = new City;
   $citi->city = $request->input('city');
   $citi->save();

   return redirect('/lugar')->with('success', 'Data Inserted');
}

View

<td> {{Form::text('city[]', '', ['class' => 'form-control name_list', 'placeholder' => 'Add Country'])}} </td>
4
  • 1
    What errors are you getting? Commented Nov 24, 2018 at 22:52
  • here, see _Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given _ Commented Nov 24, 2018 at 22:54
  • Possible duplicate of Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given Commented Nov 24, 2018 at 23:01
  • it is not laravel it is pure php. i dont understand :( Commented Nov 24, 2018 at 23:17

2 Answers 2

0

You can't store arrays in MySQL, its different if you compared it with Mongo (NoSQL).

What you can do is, turn that array into a json string. So json_encode($request->city) and save it.

Then if you want to modify it's values once retrieved from DB, you can use json_decode($data->city) which turns it back to an array.

Sign up to request clarification or add additional context in comments.

Comments

0

you can actually save this using serialize or json_encode.

Using serialize:

$citi->city = serialize($request->input('city'));

Using json_encode:

$citi->city = json_encode($request->input('city'));

then just use unserialize and json_decode on your blade file.

4 Comments

how can I unserialized it??
Arrays are now in my database but they are serialized.. how can I unserialized them? they have the value of this a:2:{i:0;s:7:"New York";i:1;s:8:"Chicago";}
hey @kapitan, all works fine except for the a:2:{i:0;s:7: for this serial code.. can i remove that and change it to normal? help me so i can upvote your answer and marked it as an asnwer ty
unserialize($myvariable) or json_decode($myvariable)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.