php - Laravel : How to store json format data in database? -
i want store json format data in database given array. how store using controller.
suppose have json data :
{"id":"bangladesh","divisions":[{"name":"barisal"},{"name":"chittagong"},{"name":"dhaka"},{"name":"khulna"},{"name":"rajshahi"},{"name":"rangpur"},{"name":"sylhet"}]}
so far know in controller using json format i'm not aware of it.
public function saveuser(request $request) { $div=new div(); $user->json = json_encode( array({"date 1": { {"id":"bangladesh","divisions":[{"name":"barisal"},{"name":"chittagong"},{"name":"dhaka"},{"name":"khulna"},{"name":"rajshahi"},{"name":"rangpur"},{"name":"sylhet"}]} $div->save(); return redirect('getlist'); }
how save in mysql list of divisions in division model using laravel controller?
you don't need convert array data json string yourself, use laravel $casts parameter on model: https://laravel.com/docs/5.2/eloquent-mutators#attribute-casting
you should in div model:
protected $casts = [ 'divisions' => 'array', ];
Comments
Post a Comment