jeudi 28 novembre 2019

How to match exact number of rows for pivot table in laravel many to many relationship?

I have table A, B , AB which is pivot table i have created respective relationship for them also. now when i want to get data which is exact match id's(array) for pivot table.

$idArray = [3,4]

A_Model::with(['B'=>function($q) use ($idArray) {
    $q->whereIn('B.id', $idArray)
}])->get;

with whereIn it gets even if match one id from array but i want to match exact id's.

example collection :

[
  0 : {
      sample_data,
      B:[
          0:{
              sample_data,
              id:2
          },
          1:{
              sample_data,
              id:3
          },
          2:{
              sample_data,
              id:4
          }

      ]
  },
  1 : {
      sample_data,
      B:[
          0:{
              sample_data,
              id:3
          },
          1:{
              sample_data,
              id:4
          },
          2:{
              sample_data,
              id:5
          }

      ]
  },
  2 : {
      sample_data,
      B:[
          0:{
              sample_data,
              id:3
          },
          1:{
              sample_data,
              id:4
          },
      ]
  },
  3 : {
      sample_data,
      B:[
          0:{
              sample_data,
              id:3
          },
          1:{
              sample_data,
              id:4
          },
      ]
  }

]

now i want following result only for $idArray = [3,4].

[
  0 : {
      sample_data,
      B:[
          0:{
              sample_data,
              id:3
          },
          1:{
              sample_data,
              id:4
          },
      ]
  },
  1 : {
      sample_data,
      B:[
          0:{
              sample_data,
              id:3
          },
          1:{
              sample_data,
              id:4
          },
      ]
  }

]


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire