Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I get all the projects while I only want the projects related to that client

I have two tables, one named Clients and the other named Projects linked together via a foreign key (this is client_id, present in Projects).

In the view related to the list of all clients, I have an additional field that shows me the number of active projects for each client.

So far everything ok! When I click on the number of active projects, I am shown all the projects while I only want those related to that client. So I believe the error is in the condition.
(the withCount below is used to provide me with the number of tasks present in each project)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

public function index($id)
    {
         $projects = Project::withCount(['tasks'=> function (Builder $query) use ($id){
        $query->where('client_id', $id);
    }])->get();
        return view('project.index', compact('projects'));
    }

There is certainly an error in the condition written above.

Can anyone kindly help me?

>Solution :

Project::where('client_id', $id)->withCount('tasks')->get();

You were trying to find the client_id inside the tasks

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading