【发布时间】:2016-02-24 23:04:25
【问题描述】:
我正在使用 Laravel 5.2 ..
有一个user 和一个profile 表,它们之间是一对一的关系,
在ProfileController,
用户登录后,
当访问create函数时,
如果用户的个人资料尚未创建,返回create页面,
否则,
重定向到edit 函数。
问题:create函数和edit函数怎么写?
我写了其中的一部分。
1、create函数中的参数id怎么写?
2、如何在edit函数中找到登录用户的个人资料?
配置文件控制器:
public function create()
{
$user = \Auth::user();
if (!$user->profile) {
return view('profile.create');
}else{
//how to write 'id'
return redirect()->action('ProfileController@edit', ['id' => .......]);
}
}
public function edit()
{
//how to find the profile of the login user?
return view('profile.edit', compact('profile'));
}
【问题讨论】:
-
我会改为在
User::create()上创建相应的个人资料记录:laravel.com/docs/master/eloquent#events -
@MartinBean 没看懂,举个例子更好。
-
@sunshine 我指给你看文档,里面有例子。