samedi 18 juillet 2020

Dynamically object record Save into database using laravel

Respected Sir/Ma'am

    I have record in below format and want to save this into 
    database,the property of this particular object is not similar often, It's changeable,
    Sometime it could be more and sometime it could be less

the property of this object is changeable,Sometime it could be more and sometime it could be less

{
    "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"
        ]);
    }

what I DO In dynamic case, I use this but not work please help me

 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/2CmozEs
via IFTTT

Aucun commentaire:

Enregistrer un commentaire