【问题标题】:How can I convert json to a Laravel Eloquent Model?如何将 json 转换为 Laravel Eloquent 模型?
【发布时间】:2017-06-23 22:49:36
【问题描述】:

如果我有一个名为 Post 的 Eloquent 模型,并且 mysql 表有:

整数 ID, 字符串文本

我如何转换这个 JSon:

{ post: { text: 'my text' } }

到相关的 Post 对象,一旦在控制器中收到,我可以像这样保存到数据库中:

public function store(Post $post)
{
    $post->save();
}

我不打算为我构建逻辑,而是为 Laravel 方式构建逻辑(或者可能没有?我用谷歌搜索它没有相关结果)。

【问题讨论】:

  • 你解决了这个问题吗?
  • 还没有,请参阅@Bartłomiej Sobieszek 回复的 cmets

标签: laravel eloquent model-binding


【解决方案1】:
  1. json转数组
  2. 从数组中水合模型

    $data = '{  
                "unique_id_001":{"name":"John","email":"JD@stackoverflow.com"},
                "unique_id_002":{"name":"Ken","email":"Ken@stackoverflow.com"}
              }';
    $object = (array)json_decode($data);
    $collection = \App\User::hydrate($object);
    $collection = $collection->flatten();   // get rid of unique_id_XXX
    
    /*
        Collection {#236 ▼
          #items: array:2 [▼
            0 => User {#239 ▶}
            1 => User {#240 ▶}
          ]
        }
     */
    dd($collection);
    

【讨论】:

  • 我得到了你演示的集合,但我需要可以保存的 Eloquent 对象。例如像这样:$collection[0]->save();
  • 我刚刚遇到了与 Zephram 相同的问题,Hydrate 函数有效,但 Hydrate 用于导入来自数据库的数组,因此“存在”。对它们调用 Save 将无济于事,因为它们并不脏:github.com/laravel/framework/issues/14288
  • 您可以将true 作为第二个参数传递,而不是将对象从json_decode() 显式转换为数组:json_decode($data, true)。诚然,它在做什么并不清楚(true 到底是如何表示“数组”的?),但它确实使代码更高效。
  • 你可以说的更清楚:json_decode($data, $array = true).
【解决方案2】:

fill 看起来像您想要的方法。为了避免将每个属性都添加到 $filled 数组中,如果您想使用 fill 方法,您需要这样做,您可以使用 the forceFill method

它接受一个关联的属性数组,因此必须对 JSON 进行解码,并且我们必须获取内部的 post 键:

$rawJson = "{ post: { text: 'my text' } }";
$decodedAsArray = json_decode($rawJson, true);
$innerPost = $decodedAsArray['post'];

一旦我们有了解码数据,我们就可以创建一个Post eloquent 模型的实例并在其上调用forceFill

$post = new Post();
$post->forceFill($innerPost);
$post->save();

这类似于做:

$post = new Post();
foreach ($innerPost as $key => $value) {
    $post->$key = $value;
}
$post->save();

【讨论】:

    【解决方案3】:

    只需将其转为数组并填充一个雄辩的

    $arr = json_decode($json, true);
    $post = new Post;
    $post->fill($arr);
    

    【讨论】:

    • Model::unguard() 之前的fill 将填满每个属性。
    【解决方案4】:

    很简单,如下:

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

    现在,你可以在最多 $post 上运行所有 eloquent 方法,例如:

    $post->save()
    

    我用 laravel v7.11.0 测试过

    【讨论】:

      【解决方案5】:

      你可以这样试试吗?

      public function store($poststuff)
      {
          $post = new Post;
          $post->text = $poststuff['text'];
          $post->save();
      }
      

      【讨论】:

      • 是的,但这还不够好,因为如果我要处理大对象,这意味着每个对象都需要大量代码。
      • 你用 foreach 来循环 $poststuff
      • 你是对的,但这更适合没有数组或内部没有更多对象的平面对象。我可以编写逻辑来处理这个问题,但正如我在原帖中提到的 - 我正在寻找一个 laravel 解决方案。
      • 不确定任何 laravel 方式,但要对 var 执行 foreach 操作,例如 $data,然后 array_push($list,$data).. 然后执行 ->insert($list);
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-19
      • 2013-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-10
      相关资源
      最近更新 更多