samedi 18 juillet 2020

Convert Object to Array using Laravel

How I convert this object to array using Laravel OR Help me to save this into database when object is dynamic

{
    "name":"nabnit jha",
    "email":"028nabnit@gmail.com",
    "password":"123456",
    "c_password":"123456",
    "imgUrl":"sonujha.jpg"
}

Simply I will do this if object property is not dynamic

public function insert($data, $modelName)
    {
        $modelName->name = $data->name;
        $modelName->email = $data->email;
        $modelName->password = $data->password;
        $modelName->img_url = $data->imgUrl;
        $modelName->save();

        return response()->json([
            'message' => "Record Inserted Successfully"
        ]);
    }

I want to save this object to database in dynamic object case, for this I use foreach loop but not work.

 public function insert($data, $modelName)
    {
        foreach ($data as $key => $value) {
            $modelName->$key = $value;
        }

        $modelName->save();

        return response()->json([
            'message' => "Record Inserted Successfully"
        ]);
    }


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3jjcpNc
via IFTTT

Aucun commentaire:

Enregistrer un commentaire