【问题标题】:Laravel 5 saving and updating models with relationshipLaravel 5 保存和更新具有关系的模型
【发布时间】:2015-08-06 05:13:17
【问题描述】:

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

我有两个模型:

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

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

和 DatosAlumno 模型:

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

如果我使用 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" => "michelle76@conroy.com"
    "datosAlumno" => array:1 [▼
        "apoderado_id" => "37"
    ]
    "password" => ""
    "password_confirmation" => ""
]

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

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

如果我在填写\Request::all() 之后执行dd($user->datosAlumno),我会得到:

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
}

【问题讨论】:

    标签: php laravel eloquent laravel-5 relationship


    【解决方案1】:

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

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

    顺便说一句,总是用英文写你的函数和变量名是一个好习惯。这样,对于在您之后将使用此代码的任何人来说,代码都更易读且易于理解。

    【讨论】:

    • 非常感谢。我会考虑你的建议。
    • 嗨,我正在尝试与您在这里所拥有的非常相似的东西:$Rental = Rental::with('region', 'location')->find($id);我可以填写直接租赁字段 $Rental->fill($requestData);并让它持续存在,但是当我尝试填充位置数据时: $Rental->location->fill($otherRequestData);它不会持续存在。我尝试了 $Rental->push()、save()、update(),但每次位置数据都不会保留。你知道为什么会发生这种情况吗?包含两个关系数据的 $Rental 对象在 Tinker 中看起来不错。
    • @mtpultz 请将您的案例作为单独的问题发布,并包含您的类声明中的一些代码示例:)
    • 嗨,我已经发布了一个单独的问题 - stackoverflow.com/q/31463650/1148107
    猜你喜欢
    • 2017-10-27
    • 2015-07-30
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    相关资源
    最近更新 更多