45

Hi I want to retrieve my projects held in a db which are owned by an auth user who also creates clients (they have many projects and tasks) and tasks (belongs to a project and tasks and user).

I want to retrieve all tasks that are not marked as closed in the status table, I know the id of this is 2 and I can retrieve this as so:

public function getOpenProjects() {

    return \Project::with(['clients', 'tasks', 'status'])
        ->where('status_id', '!=', '2')
        ->whereUserId(Auth::user()->id)
        ->get();
}

But how can I change this to query against a column in the statuses table, i.e. the name column in that table?

user0129e021939232
  • 6,205
  • 24
  • 87
  • 140

3 Answers3

105

You may try this:

$value = 'someName';
Project::with(['clients', 'tasks', 'status' => function($q) use($value) {
    // Query the name field in status table
    $q->where('name', '=', $value); // '=' is optional
}])
->where('status_id', '!=', '2')
->whereUserId(Auth::user()->id)
->get();

Also you may try this (It will fetch records only if the query returns name you want, otherwise none):

$value = 'someName';
Project::with(['clients', 'tasks', 'status'])
       ->whereHas('status', function($q) use($value) {
       // Query the name field in status table
       $q->where('name', '=', $value); // '=' is optional
})
->where('status_id', '!=', '2')
->whereUserId(Auth::user()->id)
->get();
The Alpha
  • 143,660
  • 29
  • 287
  • 307
12

You may try this:

Project::with(['clients', 'tasks' => function($q) use($value) {
// Query the name field in status table
    $q->where('status_id', '!=', '2'); 
}])
->whereUserId(Auth::user()->id)
->get();
Aatish Sai
  • 1,647
  • 1
  • 26
  • 41
-3

If you need add condition you could do

return Query::with(['department' => function ($query) use ($department) {
           $query->when($department, function ($query) use ($department) {
              $query->whereId($department);
           });
        }, 'category' => function($query) use ($category) {
            $query->when($category, function ($query) use ($category) {
              $query->whereId($category);
           });
        }])->when($activeStatus, function ($query) use ($activeStatus) {
              $activeStatus = (request()->active_status === 'active') ? 1 : 0;
              $query->whereActive($activeStatus);
         })->latest()->paginate($dataPerPage);

Or you could do

return Query::with('department', 'category')
    ->when($department, function($query) use ($department) {
        $query->whereHas('department', function (Builder $query) use ($department) {
            $query->whereId($department);
        });
    })->when($category, function ($query) use ($category) {
        $query->whereHas('category', function (Builder $query) use ($category) {
            $query->whereId($category);
        });
    })->when($activeStatus, function ($query) use ($activeStatus) {
        $activeStatus = (request()->active_status === 'active') ? 1 : 0;
        $query->whereActive($activeStatus);
    })->latest()->paginate($dataPerPage);
sh6210
  • 4,190
  • 1
  • 37
  • 27