如何将 json 转换为 Laravel Eloquent 模型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44730920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I convert json to a Laravel Eloquent Model?
提问by Zephram
if I have an Eloquent Model called Post, and the mysql table has:
如果我有一个名为 Post 的 Eloquent 模型,并且 mysql 表有:
integer ID, string Text
整数 ID,字符串 文本
How do I convert this JSon:
我如何转换这个 JSon:
{ post: { text: 'my text' } }
To the relevant Post object that, once received in the controller, I can save to the database like this:
对于相关的 Post 对象,一旦在控制器中接收到,我就可以像这样保存到数据库中:
public function store(Post $post)
{
$post->save();
}
I'm not looking to build the logic that would do that for me, but for the Laravel way (or could it be that there isn't one? I googled it with no relevant results).
我不是要构建可以为我做到这一点的逻辑,而是为 Laravel 方式(或者可能没有?我用谷歌搜索它没有相关结果)。
回答by Bart?omiej Sobieszek
- Convert json to array
Hydrate model from array
$data = '{ "unique_id_001":{"name":"John","email":"[email protected]"}, "unique_id_002":{"name":"Ken","email":"[email protected]"} }'; $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);
- 将json转换为数组
来自阵列的水合物模型
$data = '{ "unique_id_001":{"name":"John","email":"[email protected]"}, "unique_id_002":{"name":"Ken","email":"[email protected]"} }'; $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);
回答by AlbinoDrought
fill
looks like the method you want. To avoid adding every attribute to your $filled
array, which you would need to do if you wanted to use the fill
method, you can use the forceFill
method.
fill
看起来像你想要的方法。为了避免将每个属性添加到您的$filled
数组中,如果您想使用该fill
方法,您需要这样做,您可以使用该forceFill
方法。
It accepts an associative array of attributes, so the JSON will have to be decoded, and we'll have to get the inner post
key:
它接受一个关联的属性数组,因此必须对 JSON 进行解码,并且我们必须获得内部post
键:
$rawJson = "{ post: { text: 'my text' } }";
$decodedAsArray = json_decode($rawJson, true);
$innerPost = $decodedAsArray['post'];
Once we have the decoded data, we can create an instance of the Post
eloquent model and call forceFill
on it:
一旦我们有了解码的数据,我们就可以创建一个Post
eloquent 模型的实例并调用forceFill
它:
$post = new Post();
$post->forceFill($innerPost);
$post->save();
This is similar to doing:
这类似于执行以下操作:
$post = new Post();
foreach ($innerPost as $key => $value) {
$post->$key = $value;
}
$post->save();
回答by Shahaf Antwarg
Just turn it to array and fill an eloquent
只要把它变成数组并填充一个雄辩的
$arr = json_decode($json, true);
$post = new Post;
$post->fill($arr);
回答by Odinovsky
Can you try it like this?
你可以这样试试吗?
public function store($poststuff)
{
$post = new Post;
$post->text = $poststuff['text'];
$post->save();
}
回答by tanvir993
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 上运行所有 eloquent 方法,例如:
$post->save()
I tested with laravel v7.11.0
我用 laravel v7.11.0 测试过