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

时间:2016-06-29 06:30:38

标签: php class laravel-5 laravel-5.1 laravel-5.2

表是

用户表:

 id
 name
 email
 etc.

个人资料表:

 user_id
 role
 address
 city
 state
 etc.

我修补了:

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

输出是:

 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\n
     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",
 },

}

然后我做了:

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

这会引发错误:

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)'

我不知道这段代码有什么问题。我是Laravel的新手,任何形式的帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

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