vendredi 4 janvier 2019

Why does Laravel store two different array syntaxes in databases and which one is correct?

I come from a Javascript and Ruby background and this is baffling me. Laravel can store two different array syntaxes in my DB depending on how I handle my array serialization. In my understanding, collect() creates a true Laravel array. Why then is it storing a serialized array? Furthermore, is the {'key':'value'}syntax still an array despite having no square brackets surrounding it? It looks to me like a standard object or a hash, but if I try to do toArray() on it, it recognizes that it's already an array and throws an error. What am I misunderstanding and what is correct here?

Given a form:

edit.blade:

<select class="form-control m-bootstrap-select m_selectpicker" name="temp">
     <option value=>
</select>

The following two controllers syntaxes yield different database insertions.

PageController.php:

$page->cph_default = collect($request->temp);
$page->save();

Laravel stores an array with the following syntax in my database: ["{\"11\":\"1100\"}"]

PageController.php

 $page->cph_default = json_decode($request->temp, true);
 $page->save();

Laravel stores an array with the following syntax in my database: {"19": "1900"}



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2F8qgVU
via IFTTT

Aucun commentaire:

Enregistrer un commentaire