D
D
Dmitry Kuznetsov2017-09-29 11:41:34
Domain name market
Dmitry Kuznetsov, 2017-09-29 11:41:34

How to properly store JSON in Laravel?

I am saving data. Some data is stored as varchar and stored without problems, and some must be converted to json - that's the problem.
Saved like this :

{"news": "{\"add\":null,\"edit\":null,\"delete\":null,\"editMy\":null,\"deleteMy\":null,\"reason \":\"on\",\"premoderator\":\"on\"}", "comments": "{\"add\":\"on\",\"edit\":null,\ "delete\":null,\"editMy\":null,\"deleteMy\":null,\"reason\":\"on\",\"premoderator\":null}"}

And you need it like this:
{"news": {"add": false, "edit": false, "show": true, "delete": false, "editMy": false, "reason": true, "deleteMy": false, "premoderator ": true}, "comments": {"add": true, "edit": false, "show": true, "delete": false, "editMy": false, "reason": true, "deleteMy": false, "premoderator": false}}

Saving a multidimensional array :
'site_role' => json_encode([
   'news' => json_encode([
      'add' => $request->newsAdd,
      'edit' => $request->newsEdit,
      'delete' => $request->newsDelete,
      'editMy' => $request->newsEditMy,
      'deleteMy' => $request->newsDeleteMy,
      'reason' => $request->newsReason,
      'premoderator' => $request->newsPremoderator
   ]),
   'comments' => json_encode([
      'add' => $request->commentsAdd,
      'edit' => $request->commentsEdit,
      'delete' => $request->commentsDelete,
      'editMy' => $request->commentsEditMy,
      'deleteMy' => $request->commentsDeleteMy,
      'reason' => $request->commentsReason,
      'premoderator' => $request->commentsPremoderator
      ])
   ])

How to implement it correctly so that saving and output is correct.
PS: I output as follows:
In the model I do this :
protected $casts = [
   'site_role' => 'array',
   'moder_role' => 'array',
   'admin_role' => 'array',
];

In the view itself :
$role->site_role['news']['editMy']

Answer the question

In order to leave comments, you need to log in

5 answer(s)
C
CityCat4, 2019-06-19
@Mansolid

Regional zone implies a site in the language of the region, focused on visitors from the region
. Zone .com - in the language of the region and / or English, focused on visitors from anywhere
. Zone .uk - the regional zone of Britain :)
If there is an opportunity and loot, it is better to take two.

H
Hacknoob, 2019-09-17
@Hacknoob

I’ll tell you my story, now I’m creating an international site on a similar topic, Knowing this, I didn’t dare to purchase a domain in the com, ru, org, net zone,
Since it will be more of a news blog, as well as a travel blog and the like, I chose the Press zone, in my opinion, is ideal, since a diverse information site, when it was purchased, cost 200 rubles, but its current cost is $ 104. You need to understand first of all whether you are ready to further develop your site, or you are making this site for the purpose of selling ? if the second option, domain plays a big role since it is a kind of investment. The better the domain name is chosen, the more valuable the site is.

D
dsdgolik, 2020-04-26
@dsdgolik

Make one domain. If you want for different countries, then choose international. If for a specific country - regional.
I recommend an article on this topic .

I
Igor Koch, 2017-09-29
@dima9595

So why did you wrap internal arrays in json_encode?
You get json with two lines inside.
Enough site_role => json_encode(...)

K
Konstantin B., 2017-09-29
@Kostik_1993

Why do you need View if you give Json?
Directly in the controller do return $role->site_role['news']['editMy']; and don't need any view

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question