dongying6659 2015-05-14 17:08
浏览 50

无法使用eloquent更新多个数据

I am using laravel to build an application. Also, I am using Eloquent ORM.

There are two tables:

meetings - id(PK), name, meeting_with, meeting_on
user_meetings - id, meeting_id (FK -meetings (id) ), user_id

    {
      "data" : {
        "meeting_details" : {
          "id" : 8,
          "name" : "Client",
          "meeting_with" : "CEO",
          "meeting_on" : 1431588871

        },
        "user_details" : {
                "2" : "Second User",
                "1" : "First User"
          }
      }
    }

This is the JSON post that I am getting. I need to update in two tables - meetings and user_meetings

I am able to update the meetings table, but not able to update user_meetings table.

foreach($meetings_data['data']['user_details'] as $user_id => $user_name) {
            $update_array_set['user_id'] = $user_id;
            $user_meeting_result = UserMeetingDetails::where('meeting_id', '=', $meeting_id)->update($update_array_set);
}

I get the user_id from user_details.

So, if there are two users corresponding to one meeting_id, and if I try to update it to only one, it updates that, but also keeps the second entry. And, if the JSON has only one object under user_details, there needs to be only one object in database corresponding to a meeting_id

  • 写回答

2条回答 默认 最新

  • dtrvzd1171 2015-05-14 20:25
    关注

    Read this section of the documentation again: http://laravel.com/docs/5.0/eloquent#insert-update-delete

    If you set up the relationships correctly in your model, you can simply use $model->push() and it will update all the relations as well.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题