我有三张桌子:
我要做的是获取与项目相关的所有用户。
在任务模型中我有:
class Task extends \Eloquent {
protected $guarded = [];
public function users()
{
return $this->belongsToMany('\User')->withPivot('user_post_id', 'hours');
}
}
并且工作得很好。我得到了这个结果:
[0] => Array
(
[id] => 9
[created_at] => 1996-08-04 08:35:59
[updated_at] => 1991-07-27 16:36:47
[username] => xcremin
[email] => amely.wunsch@gmail.com
[remember_token] => $2y$10$WmtDoCCIA25Z/CF28KqlwOngR5vHvghD3cu0bbtPGftUV/ez2WBGe
[pivot] => Array
(
[task_id] => 1
[user_id] => 9
[user_post_id] => 10
)
)
您可以在此处查看pivot user_post_id
字段。问题是 - 我怎么能获取该字段的标题(user_post_id = 10)?
答案 0 :(得分:0)
您必须在TaskUser类中添加关系
public function userPost()
{
return $this->belongsTo('UserPost');
}
之后,您可以通过在视图中执行此操作来获取姓名
@foreach($task->users as $user)
{{ $user->pivot->userPost->title }}
@foreach
答案 1 :(得分:-1)
$user_posts = DB::table("user_post")
->whereIn("user_post_id", array($user_post_id))
->get();