【发布时间】:2018-01-27 20:24:04
【问题描述】:
在用户类模型中,我有这个
public function projects()
{
return $this->belongsToMany('App\Project');
}
在项目类模型中我有这个:
public function users(){
return $this->belongsToMany('App\User');
}
在我的 projectController 我有这个:
public function index()
{
$TeamLeader = array();
$posts = Project::orderby('id', 'desc')->with('users')->paginate(5);
return view('Projects.index', compact('posts'));
}
在我看来,每个人都有这个:
@foreach ($posts as $post)
<tr>
<td><input type="checkbox" class="checkthis" /></td>
<td href="{{ route('Projects.show', $post->project_id ) }}">{{ $post->project_id }}</td>
<td>{{ $post->name }}</td>
<td>{{ $post->description }}</td>
<td>{{ $post->created_at }}</td>
<td>{{ $user->name}}</td>
<td><p data-placement="top" data-toggle="tooltip" title="Show"><button class="btn btn-primary btn-xs" data-title="Edit" data-toggle="modal" data-target="#show" ><span class="glyphicon glyphicon-pencil"></span></button></p></td>
<td><p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-xs" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="glyphicon glyphicon-trash"></span></button></p></td>
</tr>
@endforeach
我的问题是,在我看来,我无法获取用户名,关系是多对多但我没有获取参与项目的用户名,数据库结构如您所想:
用户有 id , name , ......
项目有 id , name , location , user_id ,.....
很抱歉之前没有提到,但我也有这个公用表:
Schema::create('project_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('project_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('project_id')->references('id')->on('projects');
$table->timestamps();
});
【问题讨论】: