mercredi 7 novembre 2018

unable to store data from $request to mysql using Laravel 5.4

I am passing POST request having 3 variables Id(Randomly Generated), Name(Specified) and Capacity(Specified) for Angular Service post. These are in payload of POST, also visible in $request variable in laravel resource controller method store.

public function store(Request $request)
    {
        $servers = new Server();
        return $request->getContent(); 
    }

Here in Chrome's developer tool>network>POST request>on preview I get this

[{name: "bakar", capacity: 50, id: 8012}]
0: {name: "bakar", capacity: 50, id: 8012} 

but when I use

public function store(Request $request)
    {
        $servers = new Server();
        $data = $request->getContent();
        $servers->id = $data->id;
        $servers->name = $data->name;
        $servers->capacity = $data->capacity;
        $servers->save();
    }

above method I got an error exception stating " Trying to get property of non-object "

How can I solve this out?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire