How can I convert json to a Laravel Eloquent Model?

后端 未结 5 1545
無奈伤痛
無奈伤痛 2021-02-20 02:43

if I have an Eloquent Model called Post, and the mysql table has:

integer ID, string Text

How do I convert this JSon:

{ post: { text: \'my text\'         


        
5条回答
  •  时光取名叫无心
    2021-02-20 03:23

    It's way simple as like followings:

    $json_post = { "post": { "text": "my text" } };
    
    $post = new Post(
        json_decode($json_post, true)
    );
    

    Now, you can run all eloquent methods on the most $post, ex:

    $post->save()
    

    I tested with laravel v7.11.0

提交回复
热议问题