【发布时间】:2014-02-25 18:51:35
【问题描述】:
尝试在用户提交帖子时从用户表中检索用户名字,并将名字插入到字段作者下的帖子表中。我不认为我会正确地处理它。
后模型
public function insertPost($input)
{
$validation = new Services\Validators\Post;
if ($validation->passes())
{
$post = Post::create($input);
// Get the logged in user from the users table and save it to the posts table author field
$name = Post::get()->user->firstname;
$author = $input['author'];
$post->author = $name;
$post->save();
}
public function user()
{
return $this->belongsTo('User', 'firstname');
}
用户模型(定义到 Post 模型的关系)
public function posts()
{
return $this->hasMany('Post', 'author');
}
【问题讨论】:
标签: php model laravel laravel-4 foreign-key-relationship