Laravel 5 使用关系保存和更新模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30426194/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:37:31  来源:igfitidea点击:

Laravel 5 saving and updating models with relationship

phplaraveleloquentlaravel-5relationship

提问by eLeonZ

First thank for watch my question and sorry for my poor english.

首先感谢观看我的问题,并为我糟糕的英语感到抱歉。

I have two models :

我有两个模型:

User Model with the next relationship:

具有下一个关系的用户模型:

public function datosAlumno()
{
    return $this->hasOne('sj\Models\DatosAlumno','alumno_id');
}

and DatosAlumno model :

和 DatosAlumno 模型:

public function alumno(){
    return $this->belongsTo('sj\Models\User','alumno_id');
}

if I print with dd(\Request::all()) :

如果我用 dd(\Request::all()) 打印:

array:16 [▼
    "_method" => "PUT"
    "_token" => "vnQp4msoo8UHGan5m4v8VYvX1kKYx8HnbhqaywZH"
    "rut" => "111111111111"
    "nombre" => "AAAAAAAAAA"
    "apellido_paterno" => "BBBBBBBBBBB"
    "apellido_materno" => "CCCCCCCCCCCC"
    "sexo" => "masculino"
    "comuna_id" => "13"
    "direccion" => "RRRRRRRRRRRRRRRRRRRRRR"
    "telefono_fijo" => "14876955"
    "telefono_movil" => "3333333333"
    "fecha_nacimiento" => "2000-11-03"
    "email" => "[email protected]"
    "datosAlumno" => array:1 [▼
        "apoderado_id" => "37"
    ]
    "password" => ""
    "password_confirmation" => ""
]

I'm trying save or update "datosAlumno" but when I use save, push or update the field doesn't change in the database

我正在尝试保存或更新“datosAlumno”,但是当我使用保存、推送或更新时,数据库中的字段没有改变

public function update($id)
{
    $user = User::with('datosAlumno')->find($id);
    $user->fill(\Request::all());
    $user->push();
}

If I do dd($user->datosAlumno)after to fill with \Request::all()I get this :

如果我dd($user->datosAlumno)在填充之后这样做,\Request::all()我会得到这个:

DatosAlumno {#297 ▼
    #table: "datos_alumno"
    #fillable: array:2 [▼
        0 => "alumno_id"
        1 => "apoderado_id"
    ]
    #connection: null
    #primaryKey: "id"
    #perPage: 15
    +incrementing: true
    +timestamps: true
    #attributes: array:6 [▼
        "id" => 1
         "alumno_id" => 97
         "apoderado_id" => 15
         "deleted_at" => null
         "created_at" => "0000-00-00 00:00:00"
         "updated_at" => "0000-00-00 00:00:00"
    ]
    #original: array:6 [▼
        "id" => 1
        "alumno_id" => 97
        "apoderado_id" => 15
        "deleted_at" => null
        "created_at" => "0000-00-00 00:00:00"
        "updated_at" => "0000-00-00 00:00:00"
    ]
    #relations: []
    #hidden: []
    #visible: []
    #appends: []
    #guarded: array:1 [?]
    #dates: []
    #casts: []
    #touches: []
    #observables: []
    #with: []
    #morphClass: null
    +exists: true
}

回答by Sh1d0w

You have to set the data to the DatosAlumnomodel (hasOnerelation) before you push to database. Currently you set the data only to the user model. So this will work:

在推送到数据库之前,您必须将数据设置为DatosAlumno模型(hasOne关系)。目前,您仅将数据设置为用户模型。所以这将起作用:

public function update($id)
{
    $user = User::with('datosAlumno')->find($id);
    $user->fill(\Request::all());
    $user->datosAlumno->fill(\Request::get('datosAlumno'));
    $user->push();
}

By the way it is a good practice to ALWAYSwrite your function and variable names in English. This way the code is more readable and easy to understand for anyone that will work on this code after you.

顺便说一句,始终用英文编写函数和变量名称是一个好习惯。这样,对于在您之后处理此代码的任何人来说,代码都更具可读性和易于理解。