简体   繁体   English

如何为管理员显示每个用户的特定项目?

[英]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. 项目和用户之间的关系应该在数据库和模型本身中定义。

Database: 数据库:

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: 有了这个结论,我将选择一个抽象的表设置,如下所示:

  • table users has an id field. 表用户有一个ID字段。
  • table projects has an id field. 表格项目有一个ID字段。
  • table project_users has an id field(always required for laravel), a user_id field(foreign key to users.id) and a project_id field(foreign key to projects.id). 表project_users有一个id字段(laravel始终需要),一个user_id字段(users.id的外键)和一个project_id字段(projects.id的外键)。 Do not forget to add a unique key which combines user_id and project id, there is no need for duplicate definitions. 不要忘记添加结合了user_id和项目ID的唯一键,无需重复定义。

Model relationships: 模型关系:

Project 项目

function users()
{
    $this->belongsToMany(User::class, 'project_users', 'project_id', 'user_id');
}

User 用户

function projects()
{
    $this->belongsToMany(Project::class, 'project_users', 'user_id', 'project_id');
}

If you read this piece of documentation , it will all make sense. 如果您阅读了这份文档 ,那将是很有意义的。

Query 询问

$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. 如果没有,那么您需要在数据库中添加一条记录,以将项目与该用户耦合。

Showing results 显示结果

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;
    } 
}

Sidenote 边注

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.

相关问题 我有两种类型的用户 - 用户和管理员。 我想包括一个会话检查,以检查该人是以用户身份登录还是以管理员身份登录。 - I have two types of users - User and Admin. I would like to include a session check, to check if the person is logging on as a user or as an admin. 我如何在wp admin的woocomerce订单详细信息页面上显示自定义用户数据 - How do i show custom user data on the woocomerce order details page in wp admin 在Sonata Admin中设置表演动作时出现问题。 当我尝试使用标签选项自定义显示字段的显示标签时出错 - Problems when setting up a show action in Sonata Admin. Error when I was trying customize the displayed label of a show field using the label option 我想返回特定用户的项目? - i want to return the projects of specific user? 如何在Wordpress的管理侧栏中显示“帖子”? - How do I get “posts” to show up in the admin sidebar in wordpress? 如何为非管理员用户禁用“gii”代码生成器? - How Do I disable the “gii” code generator for non admin user? 如何显示用户登录历史? - How do I show user login history? 如何选择特定用户? - How do I select a specific user? 当我以编程方式更新属性时,它不会反映在admin上。 为什么? - When I update an attribute programatically, it doesn't reflect on admin. Why? 如果用户是特定用户OR和Admin - If user is a specific user OR and Admin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM