简体   繁体   English

如何在JSON的Eloquent ORM中保存具有相关记录的模型

[英]How to save a model with related records in Eloquent ORM from JSON

The Goal 目标

An Eloquent model, Alum , has a many-to-many relationship with School . 一个雄辩的模型, Alum ,与School多对多的关系。 One alum can have gone to many schools, and one school can have many alums. 一个明矾可以去很多学校,一所学校可以有很多校友。

Using Eloquent, I can retrieve and output an alum with her schools like this: 使用Eloquent,我可以检索并输出她的学校的明矾:

$alums = Alum::with('schools')->get();
echo $alums->toJson();

AngularJS then shows the alums in a table. 然后AngularJS在表格中显示明矾。 Each row has an edit button that shows a dialog with checkboxes for all the schools the alum could have gone to. 每行都有一个编辑按钮,显示一个对话框,其中包含明矾可能去过的所有学校的复选框。 Checking a box adds that school to the alum's schools array, and unchecking a box removes it. 选中一个框将该学校添加到明矾的schools阵列中,取消选中一个框就会删除它。

So far so good. 到现在为止还挺好。

The Problem 问题

POSTing or PUTting data back to the server is where things go wrong. 将数据发布或丢弃回服务器是出错的地方。 The JSON that's sent to the server looks like this: 发送到服务器的JSON如下所示:

{
    "id":1,
    "name":"John Doe",
    "schools": [
        {
            "id":1,
            "name":"School A"
        },
        {
            "id":2,
            "name":"School B"
        }
    ]
}

The Alum record saves, but Eloquent doesn't know what to do with the array of schools. Alum记录保存,但Eloquent不知道如何处理这些学校。

Attempted Solution 试图解决方案

From Laravel's documentation on Inserting Related Models in Eloquent , it looks like the way to save related records is to use attach . Laravel关于Eloquent中插入相关模型的文档中 ,看起来保存相关记录的方法是使用attach attach takes an array of IDs, not objects, so I try the following: attach需要一组ID,而不是对象,所以我尝试以下方法:

// Get the IDs of the schools that this user attended
$schoolIds = [];
foreach ($obj->schools as $school) {
    $schoolIds[] = $school->id;
}

$alum->schools()->attach($schoolIds);
$alum->save();

That works; 这样可行; the alum and his schools are saved to the database. 明矾和他的学校被保存到数据库中。 But then there's a new problem: how to detach schools when they're unchecked from an alum's school list. 但后来又出现了一个新问题:当学校从校友的学校名单中取消学校时,如何分开学校。 I suppose I could get the IDs of all schools, splice out the ones in the array of checked schools, and pass the result to detach —but I hope there's a more elegant way than that. 我想我可以获得所有学校的ID,拼出一系列托运学校的ID,并将结果传递给detach - 但我希望有一种更优雅的方式。

What's the best way to save a record and its related records from JSON? 从JSON保存记录及其相关记录的最佳方法是什么?

Funny how posting seems to lead you straight to finding a solution on your own. 有趣的发布似乎让你直接找到自己的解决方案。 I had missed sync in the Eloquent docs: 我在Eloquent文档中错过了sync

The sync method accepts an array of IDs to place on the pivot table. sync方法接受要放置在数据透视表上的ID数组。 After this operation is complete, only the IDs in the array will be on the intermediate table for the model[.] 完成此操作后,只有数组中的ID将位于模型[。]的中间表上

After replacing attach with sync, only the checked schools are saved in the alum's school list. 用同步替换attach后,只有经过检查的学校才会保存在校友的学校名单中。

I'm still curious if there's a better way to save related records straight from JSON, though. 不过,如果有更好的方法直接从JSON保存相关记录,我仍然很好奇。 More input is welcomed. 欢迎提供更多意见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM