Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tehseen's avatar

author and product relationship query issue

I want to get all author with their product at least 5 but the query is not working

$users = User::with(['products' => function ($query) {
            $query->limit(6);
        }])->get();

the above query will return something like below

Collection {#382 ▼
  #items: array:5 [▼
    0 => User {#412 ▼
      #fillable: array:3 [▶]
      +sortable: array:3 [▶]
      #hidden: array:2 [▶]
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:15 [▶]
      #original: array:15 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▶]
      #touches: []
      +timestamps: true
      #visible: []
      #guarded: array:1 [▶]
      #rememberTokenName: "remember_token"
      -roleClass: null
      -permissionClass: null
    }
    1 => User {#413 ▶}
    2 => User {#414 ▶}
    3 => User {#415 ▶}
    4 => User {#416 ▶}
  ]
}

The first user only have product not all, but in my database both first and second author have product

Its relationship between user/author and products one to many

in my user modal

public function products() {
        return $this->hasMany('App\Product');
    }

in my product modal

 public function user() {
        return $this->belongsTo('App\User', 'user_id');
    }

and in database we have user_id in product table.

Please help

0 likes
14 replies
tisuchi's avatar

@tehseen

Does take() works for you?

$users = User::with(['products' => function ($query) {
            $query->take(6);
        }])->get();
tehseen's avatar

@tisuchi

Thanks for reply, the above query will get all user but in relationship for the second user we have 1 product (but second user have 5 products)

Collection {#382 ▼
  #items: array:5 [▼
    0 => User {#412 ▶}
    1 => User {#413 ▼
      #fillable: array:3 [▶]
      +sortable: array:3 [▶]
      #hidden: array:2 [▶]
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:15 [▶]
      #original: array:15 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: array:1 [▼
        "products" => Collection {#411 ▼
          #items: array:1 [▼
            0 => Product {#430 ▼
              #fillable: array:4 [▶]
              +sortable: array:4 [▶]
              #connection: "mysql"
              #table: null
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:27 [▶]
              #original: array:27 [▶]
              #changes: []
              #casts: []
              #dates: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: []
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #guarded: array:1 [▶]
            }
          ]
        }
      ]
      #touches: []
      +timestamps: true
      #visible: []
      #guarded: array:1 [▶]
      #rememberTokenName: "remember_token"
      -roleClass: null
      -permissionClass: null
    }
    2 => User {#414 ▶}
    3 => User {#415 ▶}
    4 => User {#416 ▶}
  ]
}
tisuchi's avatar

@tehseen

Does your relationship work properly?

Wouldn't it be like that?

public function products() {
        return $this->hasMany('App\Product', 'user_id');
    }
tehseen's avatar

@tisuchi

in my user modal

public function products() {
        return $this->hasMany('App\Product');
    }

in my product modal

public function user() {
        return $this->belongsTo('App\User', 'user_id');
    }

is something wrong ? can set user_id in user model not product ?

tisuchi's avatar

It seems something wrong with foreign and local key, if I am not mistaken. Can you show your tow tables, products and users?

tisuchi's avatar

@tehseen

In your product model, you don't have to define the foreign key then. You just use this-

public function user() {
        return $this->belongsTo('App\User');
    }

and then can you try again?

tehseen's avatar

@tisuchi

i remove the user_id in my product model but still i get 5 products for user one and 1 product for user 2 but we have 5 for user too

here is the query

$productWithUser = User::with(['products' => function ($query) {

            $query->take(6);
        }])->has('products')->get()->toarray();

A4Family's avatar

@tehseen What if you simply try this query?

User::with(['products' => function ($query) {
    $query->take(6);
}])->get();

Does it return the right data?

tisuchi's avatar

@tehseen Dou really need the toArray()? If not, you can ignore it as @a4family suggested.

Otherwise, try this.

User::with(['products' => function ($query) {
    $query->take(6);
}])->get()->toArray();
tehseen's avatar

@a4family

same always get 5 product for only 1 user and then one product for 2.

Collection {#382 ▼
  #items: array:5 [▼
    0 => User {#412 ▶}
    1 => User {#413 ▶}
    2 => User {#414 ▶}
    3 => User {#415 ▶}
    4 => User {#416 ▶}
  ]
}
tehseen's avatar

@tisuchi

still the same, but the issue is why we get the all user who don't have any product i only want to get user with products and limit (fetch first 5 or 6 products)

tisuchi's avatar

@tehseen You have changed your mind. Previously the condition was not like that.

i only want to get user with products

The following code will get all the users who have at least one product. Then it will take maximum of 6 products who have more than that.

User::with('products')
->whereHas('products', function ($query) {
    $query->take(6);
})->get();

Please or to participate in this conversation.