doubu7134 2016-06-29 06:30
浏览 172

在Laravel 5.2中更新一对一模型的问题

Tables are

User Table:

 id
 name
 email
 etc.

Profile Table:

 user_id
 role
 address
 city
 state
 etc.

On tinker i did:

 $user = App\User::with('profile')->find(3);

Output is:

 App\User {#756
 id: 3,
 name: "Alejandra Kerluke Jr.",
 email: "balistreri.laurel@example.com",
 deleted_at: null,
 created_at: "2016-06-23 05:12:03",
 updated_at: "2016-06-29 11:05:45",
 Profile: App\Profile {#768
   user_id: 3,
   role: "",
   address: """
     50111 Wendy Row Apt. 732

     Kingburgh, OR 42164-7189
     """,
   city: "Manteland",
   state: "Vermont",
   country: "Belgium",
   phone: "1-226-766-4182 x5574",
   fax: "1-507-985-3523 x708",
   zip: 48446,
   status: 0,
   deleted_at: null,
   created_at: "2016-06-23 05:12:03",
   updated_at: "2016-06-23 05:12:03",
 },

}

Then i did:

>>> $user->name="YYYYYY"
>>> $user->email='abc@example.com'
>>> $user->profile->role = 'XXXXX'
>>> $user->push();

This throws the error:

Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update `profiles` set `role` = inspector, `updated_at` = 2016-06-29 11:29:30 where `id` is null)'

I don't know what's wrong with this piece of code. I am new to Laravel and any kind of help will be appreciated.

  • 写回答

1条回答 默认 最新

  • dongtun2459 2016-07-13 06:42
    关注

    You can try this:

    $user = App\User::with('profile')->get()->find(3);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器