-2

My project runs on PHP 8.1.18 Laravel 9.52.7 that uses Mongodb driver (https://github.com/jenssegers/laravel-mongodb).

Project::with(['skills' => function($q) {$q->select('_id', 'name');}])->first();

The following is part of the exception I get:

enter image description here

James Z
  • 12,209
  • 10
  • 24
  • 44
felixkpt
  • 128
  • 3
  • 10
  • what version of laravel-mongodb? – lagbox May 30 '23 at 20:50
  • 3.8 or 3.9 both have same issue – felixkpt May 30 '23 at 21:03
  • if you don't try to limit the fields returned for the relationship it does work, just a regular `with('skills')`? – lagbox May 30 '23 at 23:01
  • Surely it works perfectly, problem is when I limit, of which this is my desired outcome! – felixkpt May 30 '23 at 23:45
  • this may be related to an old issue with that package ... considering this is 1 record you could remove the `select` for now – lagbox May 31 '23 at 01:07
  • I have approximately 50 projects, I have done pagination, but still the response is too large for listing purpose I don't need the "content" column among others from the "projects" table and "skills" table. – felixkpt May 31 '23 at 04:31

1 Answers1

0

I see there r guys who Jump into just pulling down peoples reputation for asking questions. But anyway here is how I worked around the problem in case anyone faces the same:

The controller listing method:

/**
 * return project's index view
 */
public function index()
{
    $items = Project::where([])->with(['company', 'skills']);

    if (request()->all)
        return $this->select($items->limit(4)->get());

    $items = $items->paginate();

    $items_only = $items->getCollection();
    $res = $this->select($items_only);
    $items->setCollection($res);

    return response(['message' => 'success', 'data' => $items]);
}

The selecting method:

private function select($q)
{
    return $q->map(
        function ($q) {
            return [
                ...$q->only([
                    '_id',
                    'title',
                    'slug',
                    'featured_image',
                    'start_date',
                    'end_date',
                    'status',
                    'importance'
                ]),
                'company' => $q->company()->first(['name']),
                'skills' => $q->skills()->get(['name'])
            ];
        }
    );
}

NB: Anyone to kindly show me how to solve this better.

felixkpt
  • 128
  • 3
  • 10