0

I'm a relatively new PHP developer, so I might get some of this wrong.

PHP: 7.4 Laravel/Lumen: 7.0 Fractal: 0.13-dev (I think)

I have a model, Programs.

<?php

use Illuminate\Database\Eloquent\Model;

class Program extends Model
{
    public $guarded = ['id'];

    public function sites()
    {
        return $this->belongsToMany(Site::class, 'programs_sites');
    }
}

?>

which has a belongsToMany relationship with Sites.

?>

<?php

use Illuminate\Database\Eloquent\Model;

class Site extends Model
{
    public $guarded = ['id'];
}

?>

I'm able to use this ProgramController method:

public function index(Request $request) {
    $query = Program::withCount('sites')->whereClientId($request->get('client_id'));

    $products = $query->get();

    return $this->fractal->respondWithCollection($products, new ProgramTransformer);
}

to return the nested relationship from the API with the following URL: http://local:8081/ads/api/programs?client_id=130785&includes%5B0%5D=sites

{
    "data": [
        {
            "id": 817,
            "name": "Daugherty Ltd",
            "sites": [
                {
                    "id": 13,
                    "name": "MyDomain.com",
                },
            ],
        }
    ],
    "meta": {
        "count": 1
    }
}

When I remove the includes query param, the sites block drops off as expected. However my front end also needs counts of the nested relationships. I understand that I could simply run a count of the sites array, but I'd like to get it from the service if possible.

The Laravel docs indicate that there's a withCount which "will place a {relation}_count column on your resulting models". However, in my case it's not working. When hitting this via Postman I'd expect to see an additional property on the Program model, "site_count": 1. But I'm not seeing any additional properties, and no errors either.

Given my inexperience with PHP / Laravel my first assumption is that I'm doing something wrong, but I don't know what it might be. Can anyone offer insight into what I'm missing?

commadelimited
  • 5,656
  • 6
  • 41
  • 77
  • Does the `Site` model have the inverse relationship to `Program` properly defined? Also note the relationship is `sites`, so the column name would be `sites_count` and not `site_count`. – miken32 Oct 09 '20 at 16:54
  • The `Site` model is posted above, with no inverse relationship. But I am able to get Site info in the Program model right now. I'm just not seeing sites_count (thanks for the clarification on that). – commadelimited Oct 09 '20 at 16:58
  • You should add the other side of the relationship to the `Site` model. Also I've not used Fractal, have you tried a `dd($products)` in your controller method, to make sure that it (Fractal) isn't dropping the column for some reason? – miken32 Oct 09 '20 at 16:58
  • hie, I just tested your code. This should return site_count without a doubt. Could you print us the result of `dd($products);` – Deepesh Thapa Oct 09 '20 at 17:23
  • May I also know if you are using which laravel package for fractal? – Deepesh Thapa Oct 09 '20 at 17:30
  • @DeepeshThapa I added some relevant pacakges/versions to the original post. Outputting `dd($products)` is over 800 lines of code. I searched through it though and found no references to `sites_count`, although there were several mentions of `withCount`. – commadelimited Oct 09 '20 at 18:47
  • You can limit the search result of `$product` to 1 by doing `$products = $query->limit(1)->get();` and show us the result. It is very relevant to your question. – Deepesh Thapa Oct 09 '20 at 19:03
  • Ohh Additional thought here. On Using Lumen [Micro Framework] so from the docs - If you would like to use the Eloquent ORM, you should uncomment the `$app->withEloquent()` call in your `bootstrap/app.php` file. – Deepesh Thapa Oct 09 '20 at 19:14
  • Doing `dd($products->toArray())` might give you a more manageable output. – miken32 Oct 09 '20 at 20:38

0 Answers0