[英]How do I show specific projects of each user for the admin.?
The admin of the application has access to list each projects of a user. 应用程序的管理员有权列出用户的每个项目。 Write now I am able to list the projects but they all are listed together no matter the unique id of the URL.
现在写,我能够列出项目,但是无论URL的唯一ID,它们都一起列出。
// Admin form case controller
public function adminforms (Project $project){
$users = User::get();
return view('smiledesign.adminforms', compact('users', 'project'));
}
///Records controller
public function records(user $users, project $project){
$project = project::get();
$users = user::get();
return view ('/smiledesign/records' , compact( 'users','project'));
}
<table class="table">
<thead>
<tr>
<th>Dr Name</th>
<th>User Id</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach ($users as $user)
<tr>
<td> <a href="/smiledesign/{{$user->id}}/records">{{$user->name}}</a></td>
<td> <a href="/smiledesign/{{$user->id}}/records">{{$user->id}}</a></td>
<td> <a href="/smiledesign/{{$user->id}}/records">{{$user->email}}</a></td>
</tr>
</tbody>
@endforeach
</table>
// Records view
<table class="table table-striped table-bordered" id="table_id">
<thead>
<tr>
<th>Case Number</th>
<th>Case Form</th>
<th>Patient Name</th>
<th>Date Created</th>
<th>Status</th>
</tr>
</thead>
@foreach ($project as $project)
<tbody>
<tr>
<td> <a href="/smiledesign/{{$project->id}}/show">{{$project->case_number}}</a></td>
@if ($project->services0)
<td> <a href="/smiledesign/{{$project->id}}/show">{{$project->services0}}</a></td>
@elseif ($project->services1)
<td> <a href="/smiledesign/{{$project->id}}/show">{{$project->services1}}</a></td>
@elseif ($project->services2)
{{-- <td> <a href="/smiledesign/{{$project->id}}/show">{{$project->services2 . ' ' . $project->mockup0}}</a></td> --}}
<td> <a href="/smiledesign/{{$project->id}}/show">{{$project->services2 . ' ' . $project->mockup0 . ' ' . $project->mockup1}}</a></td>
@endif
<td> <a href="/smiledesign/{{$project->id}}/show">{{$project->first_name . ' ' . $project->last_name}}</a></td>
<td> <a href="/smiledesign/{{$project->id}}/show">{{$project->created_at}}</a></td>
<td> <a href="/smiledesign/{{$project->id}}/show">{{$project->concerns}}</a></td>
</tr>
</tbody>
@endforeach
</table>
What I wanna see is each project of a specific user when I click The link from the adminforms user. 当我单击adminforms用户的链接时,我想看到的是特定用户的每个项目。 Instead, every link I go I see all projects of all users
取而代之的是,我访问的每个链接都可以看到所有用户的所有项目
The relationship between the projects and users should be defined in the database and in the models themselves. 项目和用户之间的关系应该在数据库和模型本身中定义。
In my interpretation, a user can work on multiple projects and a project can have multiple users, this would be a many to many relationship. 按照我的解释,一个用户可以从事多个项目,一个项目可以有多个用户,这将是多对多关系。 With this conclusion, I would go for an abstracted table setup like so:
有了这个结论,我将选择一个抽象的表设置,如下所示:
function users()
{
$this->belongsToMany(User::class, 'project_users', 'project_id', 'user_id');
}
function projects()
{
$this->belongsToMany(Project::class, 'project_users', 'user_id', 'project_id');
}
If you read this piece of documentation , it will all make sense. 如果您阅读了这份文档 ,那将是很有意义的。
$users = User::with('projects')->get();
or 要么
$projects = Project:with('users')->get();
Now every user instance has a projects collection. 现在,每个用户实例都有一个项目集合。 If they do not, then you need to add a record to the database coupling a project to that user.
如果没有,那么您需要在数据库中添加一条记录,以将项目与该用户耦合。
foreach($users as $user)
{
foreach($user->projects as $project)
{
echo "$user->name works on project $project->name" . PHP_EOL;
}
}
or 要么
foreach($projects as $project)
{
foreach($project->users as $user)
{
echo "$user->name works on project $project->name" . PHP_EOL;
}
}
My instructions are optimized for performance. 我的说明针对性能进行了优化。 You could also just call
->projects
on the $user
without using the with('projects')
in the query, but that will mean that you will trigger a query on the spot. 您也可以只在
$user
上调用->projects
而不在查询中使用with('projects')
,但这意味着您将立即触发查询。 As this kind of code is prone to being used in foreach
statements, you could be doing queries in foreach
statements which is referred to a N+1 query problem . 由于此类代码易于在
foreach
语句中使用,因此您可能正在foreach
语句中进行查询,这被称为N + 1查询问题 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.