0

in CitizenService there is service_id but this query returning query: {} in postman. in model connected service();

$query = CitizenService::whereHas('service', function (Builder $query)  {
        $query->where('service.id', 1);
    });
    return response()->json(['query' => $query]);
Farrux Choriyev
  • 179
  • 1
  • 5

3 Answers3

0

You need to use id instead of service.id inside the where() clause.

Can you try this:

$query = CitizenService::whereHas('service', function (Builder $query)  {
    $query->where('id', 1);
});
return response()->json(['query' => $query]);
Amit Senjaliya
  • 2,867
  • 1
  • 10
  • 24
0

You need to actually execute the query. You have built a query, but have not ran it.

$result = $query->get();

Or with your code:

$query = CitizenService::whereHas('service', function ($query)  {
    $query->where('service.id', 1);
})->get();

return response()->json(['query' => $query]);

You don't need to type-hint the argument to the Closure, but if you do make sure it is Illuminate\Database\Eloquent\Builder.

lagbox
  • 48,571
  • 8
  • 72
  • 83
  • then you have to figure out what that error is ... you have not executed your query, you were just returning a query builder not a result – lagbox Dec 02 '19 at 11:02
0

Seems like you are returning a query builder

Look at this

$query = CitizenService::whereHas('service', function ($query)  {
    $query->where('id', 1);
})->get();

return response()->json(['query' => $query]);

Make sure you defined the proper relationship

For more information querying on relationship

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
bhavinjr
  • 1,663
  • 13
  • 19