<legend id='9WnN3'><style id='9WnN3'><dir id='9WnN3'><q id='9WnN3'></q></dir></style></legend>
      <bdo id='9WnN3'></bdo><ul id='9WnN3'></ul>
    1. <small id='9WnN3'></small><noframes id='9WnN3'>

      <i id='9WnN3'><tr id='9WnN3'><dt id='9WnN3'><q id='9WnN3'><span id='9WnN3'><b id='9WnN3'><form id='9WnN3'><ins id='9WnN3'></ins><ul id='9WnN3'></ul><sub id='9WnN3'></sub></form><legend id='9WnN3'></legend><bdo id='9WnN3'><pre id='9WnN3'><center id='9WnN3'></center></pre></bdo></b><th id='9WnN3'></th></span></q></dt></tr></i><div id='9WnN3'><tfoot id='9WnN3'></tfoot><dl id='9WnN3'><fieldset id='9WnN3'></fieldset></dl></div>
      <tfoot id='9WnN3'></tfoot>

    2. 如何在 Laravel 5 中使用 Eloquent 更新数据透视表

      How to update a pivot table using Eloquent in laravel 5(如何在 Laravel 5 中使用 Eloquent 更新数据透视表)

          <i id='9vINp'><tr id='9vINp'><dt id='9vINp'><q id='9vINp'><span id='9vINp'><b id='9vINp'><form id='9vINp'><ins id='9vINp'></ins><ul id='9vINp'></ul><sub id='9vINp'></sub></form><legend id='9vINp'></legend><bdo id='9vINp'><pre id='9vINp'><center id='9vINp'></center></pre></bdo></b><th id='9vINp'></th></span></q></dt></tr></i><div id='9vINp'><tfoot id='9vINp'></tfoot><dl id='9vINp'><fieldset id='9vINp'></fieldset></dl></div>
              <tbody id='9vINp'></tbody>
          • <legend id='9vINp'><style id='9vINp'><dir id='9vINp'><q id='9vINp'></q></dir></style></legend>

            <small id='9vINp'></small><noframes id='9vINp'>

          • <tfoot id='9vINp'></tfoot>
            • <bdo id='9vINp'></bdo><ul id='9vINp'></ul>
                本文介绍了如何在 Laravel 5 中使用 Eloquent 更新数据透视表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我是 Laravel 的新手.我正在开发一个 Laravel 5 应用程序,但我被困在这里.我有 2 个这样的模型:

                I am new to laravel. I am working on a laravel 5 app and I am stuck here. I have 2 models as such:

                class Message extends Eloquent{
                
                    public function user()
                    {
                        return $this->belongsTo('AppUser', 'from');
                    }
                
                    public function users()
                    {
                        return $this->belongsToMany('AppUser')->withPivot('status');
                    }
                }
                
                class User extends Eloquent {
                
                    public function messages()
                    {
                        return $this->hasMany('AppMessage', 'from');
                    }
                
                    public function receive_messages() {
                        return $this->belongsToMany('AppMessage')->withPivot('status');
                    }
                }
                

                Message 和 User 之间存在多对多关系,为我提供了一个数据透视表:

                There exist a many-to-many relationship between Message and User giving me a pivot table as such:

                Table Name: message_user
                Colums:
                message_id
                user_id
                status
                

                我有一个这样的 SQL 查询:

                I have an SQL query as such:

                update message_user
                set status = 1
                where user_id = 4 and message_id in (select id from messages where message_id = 123)
                

                如何将此查询转换为 Laravel 等效项?

                How can I translate this query to the laravel equivalent?

                推荐答案

                下面的代码解决了我的问题:

                The code below solved my problem:

                $messages  = Message::where('message_id', $id)->get();
                foreach($messages as $message)
                   $message->users()->updateExistingPivot($user, array('status' => 1), false);
                

                这篇关于如何在 Laravel 5 中使用 Eloquent 更新数据透视表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to update a record using sequelize for node?(如何使用节点的 sequelize 更新记录?)
                How to provide a mysql database connection in single file in nodejs(如何在 nodejs 中的单个文件中提供 mysql 数据库连接)
                Looping Over Result Sets in MySQL(在 MySQL 中循环结果集)
                Writing an SQL query to SELECT item from the following table(编写 SQL 查询以从下表中选择项目)
                Converting MySQL code to Access: GROUP_CONCAT and a triple JOIN(将 MySQL 代码转换为 Access:GROUP_CONCAT 和三重 JOIN)
                How can I convert an MDB (Access) file to MySQL (or plain SQL file)?(如何将 MDB (Access) 文件转换为 MySQL(或普通 SQL 文件)?)
                  <legend id='5dRyH'><style id='5dRyH'><dir id='5dRyH'><q id='5dRyH'></q></dir></style></legend>

                      <tbody id='5dRyH'></tbody>

                      • <i id='5dRyH'><tr id='5dRyH'><dt id='5dRyH'><q id='5dRyH'><span id='5dRyH'><b id='5dRyH'><form id='5dRyH'><ins id='5dRyH'></ins><ul id='5dRyH'></ul><sub id='5dRyH'></sub></form><legend id='5dRyH'></legend><bdo id='5dRyH'><pre id='5dRyH'><center id='5dRyH'></center></pre></bdo></b><th id='5dRyH'></th></span></q></dt></tr></i><div id='5dRyH'><tfoot id='5dRyH'></tfoot><dl id='5dRyH'><fieldset id='5dRyH'></fieldset></dl></div>
                          <bdo id='5dRyH'></bdo><ul id='5dRyH'></ul>

                          <small id='5dRyH'></small><noframes id='5dRyH'>

                          <tfoot id='5dRyH'></tfoot>