douzhao7014 2017-04-05 13:28
浏览 132

更新与Laravel 5.4上由hasOne关系链接的模型相关的字段

I have 2 tables, person and physicalperson. The person table is a general table, and the physicalperson is a specific person related to a human (because we can also have moralperson that represent a company). These two tables are linked by a OnetoOne relationship.

Table "person" :

id  |   name    |   comments    |   created_at
==============================================
1       Doe                         2017-03-30
2       Bar         Test            2017-04-05

Table "physicalperson" :

id  |   person_id   |   surname |   birth
==============================================
1       1               John        1990-07-31
2       2               Foo         1970-01-01

Now I set up my relations ships, saying that for 1 person I have maximum 1 physical person :

<?php
    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Person extends Model
    {
        public $timestamps = false;

        protected $table = 'person';    
        protected $primaryKey = 'id';

        public function physical()
        {
            return $this->hasOne('App\PhysicalPerson');
        }
    }
?>

I confirm, in selection, I get all the data I need by doing a :

<?php
    $person = Person::findOrFail(1);

    echo $person->physical->surname; // Prints John
    echo $person->name; // Prints Doe
?>

But when I try to update my person (and the associated physicalperson), I only managed to get the update for the person, and the physical seems to be ignored for an unknown reason :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Person extends Model
{
    public $timestamps = false;

    protected $table = 'person';    
    protected $primaryKey = 'id';

    public function physical()
    {
        return $this->hasOne('App\PhysicalPerson');
    }

    public static function put( $id, Request $request )
    {
        $person = self::findOrFail( $id );

        $person->name = $request->input('name');
        $person->physical->surname = $request->input('surname'); // Seems to be ignored

        $person->save();
    }
}

Question

Does anyone already managed to updated a model "in cascade" using the Eloquent relationships methods ?

  • 写回答

1条回答 默认 最新

  • douxiao0400 2018-10-10 12:29
    关注
    $person = self::findOrFail( $id );
     $physicalPersion = physicalperson::where('persion_id' , '=' , $id)->get() ;
        $person->name = $request->input('name');
        $physicalPersion->surname = $request->input('surname'); 
        $physicalPersion->save() ;
        $person->save();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c