Ap3twe
145
7
Laravel

How do I give access to one user (admin) to view all other peoples projects?

Posted 2 weeks ago by Ap3twe

In my app. All users can create a task. The admin can create a task too. The client side is all finish. Now am creating the admin. I have been able to restrict all views that belong to the Admin. How do I enable the Admin to see other users tasks created? I don't want the admin to see the tasks in the same view as the user. I have routes for that. Now I have been able to list all users on a table but when I click a user I see all tasks of all users. I want to see all specific task of the user I click.

I get all users correctly here

//    Admin form case
    public function adminforms (Project $project){
        $users = User::get();
      
     return view('design.adminforms', compact('users'));
 }

Now here I get all projects of all users when I click on the user. I want to only projects from that user not everyone

        $project = project::get();
        return view ('/design/records' , compact('project'));
    }

adminForms View


     <h1 class="text-center m-4"> 
       Doctors Cases  
     </h1>
     <table class="table">
            <thead>
                    <tr>
                            <th>Dr Name</th>
                            <th>User Id</th>
                            <th>Clinic</th>
                    </tr>
            </thead>
            <tbody>
       @foreach ($users as $user)
             <tr>
                 <td> <a href="/smiledesign/records">{{$user->name}}</a></td>
                 <td> <a href="/smiledesign/records">{{$user->id}}</a></td>
                 <td> <a href="/smiledesign/records">{{$user->name}}</a></td>
             </tr>

         </tbody>
         @endforeach
     </table>   
@endsection

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>
            <tbody>
       @foreach ($project as $project)
             <tr>
                
                 <td> <a href="/design/{{$project->id}}/show">{{$project->case_number}}</a></td>
                
                 @if ($project->services0)
                 <td> <a href="/design/{{$project->id}}/show">{{$project->services0}}</a></td> 
                 @elseif ($project->services1)
                 <td> <a href="/design/{{$project->id}}/show">{{$project->services1}}</a></td> 
                 @elseif ($project->services2)
                 {{-- <td> <a href="/design/{{$project->id}}/show">{{$project->services2 . '  '  . $project->mockup0}}</a></td>  --}}
                 <td> <a href="/design/{{$project->id}}/show">{{$project->services2 . '  '  . $project->mockup0 . ' ' . $project->mockup1}}</a></td> 

                 @endif
                 <td> <a href="/design/{{$project->id}}/show">{{$project->first_name . ' ' . $project->last_name}}</a></td>
                 <td> <a href="/design/{{$project->id}}/show">{{$project->created_at}}</a></td>
                 <td> <a href="/design/{{$project->id}}/show">{{$project->concerns}}</a></td> 
             </tr>

         </tbody>
         @endforeach
     </table>
         ```
Policy

```class ProjectPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can view the project.
     *
     * @param  \App\User  $user
     * @param  \App\Project  $project
     * @return mixed
     */
    public function view(User $user, Project $project)
    {
        return $project->user_id == $user->id;
    }

    /**
     * Determine whether the user can update the project.
     *
     * @param  \App\User  $user
     * @param  \App\Project  $project
     * @return mixed
     */
    public function update(User $user, Project $project)
    {
        return $project->user_id == $user->id;
    }

    public function show(User $user, Project $project)
    {
        return $project->user_id == $user->id;
    }
    /**
     * Determine whether the user can delete the project.
     *
     * @param  \App\User  $user
     * @param  \App\Project  $project
     * @return mixed
     */
    public function delete(User $user, Project $project)
    {
        return $project->user_id == $user->id;
    }

    /**
     * Determine whether the user can restore the project.
     *
     * @param  \App\User  $user
     * @param  \App\Project  $project
     * @return mixed
     */
  }```

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.