laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

Selecting specfic column in with relation is not working

Open HassanKareem opened this issue 2 years ago • 1 comments

  • Laravel-mongodb Version: 3.8^
  • PHP Version: 7.4.28
  • Database Driver & Version:

Description:

This is my code

Product::query()->with('sales', function ($query){ $query->select('unit_count', 'total_sales', 'date'); });

Expected behaviour

The Expected behaviour is that I must have all products with their sales ( Product Has Many Sales) and custom columns only on the sales table.

Actual behaviour

It returns an empty sales array when I try to select specific columns only.

@jenssegers

HassanKareem avatar Apr 12 '22 21:04 HassanKareem

i have the same issue

my code using $request from HTTP Request

    **$query = LogAdmin::query();
    if ($request->admin_id) {
        $query->where('adm_id', $request->admin_id);
    }
   return $query->get();**

but it will runs if like this return LogAdmin::where('admin_id', 8)->get(); @jenssegers

yogiepratama1 avatar Jul 31 '22 15:07 yogiepratama1

needs to add reference ID in select

Product::query()->with('sales', function ($query){
$query->select('unit_count', 'total_sales', 'date', 'product_id');
});

BilalTariq01 avatar Dec 16 '23 17:12 BilalTariq01

Closing as the solution is to retrieve the relationship fields, as mentioned by @BilalTariq01.

GromNaN avatar Dec 17 '23 11:12 GromNaN