@@ -562,6 +562,78 @@ public function test_getting_a_list_of_resources_including_belongs_to_many_relat
562
562
['Accept ' => 'application/json ' ]
563
563
);
564
564
565
+ $ this ->assertResourcePaginated (
566
+ $ response ,
567
+ [$ matchingModel , $ matchingModel2 ],
568
+ new ModelResource (),
569
+ [
570
+ [
571
+ 'belongs_to_many_relation ' => $ matchingModel ->belongsToManyRelation ()
572
+ ->orderBy ('id ' , 'desc ' )
573
+ ->get ()
574
+ ->map (function ($ relation ) use ($ pivotAccessor ) {
575
+ return collect ($ relation ->only (
576
+ array_merge ((new BelongsToManyResource ())->getFields (app ()->make (RestRequest::class)), [$ pivotAccessor ])
577
+ ))
578
+ ->pipe (function ($ relation ) use ($ pivotAccessor ) {
579
+ $ relation [$ pivotAccessor ] = collect ($ relation [$ pivotAccessor ]->toArray ())
580
+ ->only (
581
+ (new ModelResource ())->relation ('belongsToManyRelation ' )->getPivotFields ()
582
+ );
583
+
584
+ return $ relation ;
585
+ });
586
+ })
587
+ ->toArray (),
588
+ ],
589
+ [
590
+ 'belongs_to_many_relation ' => [],
591
+ ],
592
+ ]
593
+ );
594
+ }
595
+
596
+ public function test_getting_a_list_of_resources_including_belongs_to_many_relation_and_filtering_on_pivot_with_null_value (): void
597
+ {
598
+ $ matchingModel = ModelFactory::new ()
599
+ ->hasAttached (
600
+ BelongsToManyRelationFactory::new ()->count (1 ),
601
+ ['number ' => null ],
602
+ 'belongsToManyRelation '
603
+ )
604
+ ->create ()->fresh ();
605
+
606
+ $ matchingModel2 = ModelFactory::new ()
607
+ ->hasAttached (
608
+ BelongsToManyRelationFactory::new ()->count (1 ),
609
+ ['number ' => 1 ],
610
+ 'belongsToManyRelation '
611
+ )
612
+ ->create ()->fresh ();
613
+
614
+ $ pivotAccessor = $ matchingModel ->belongsToManyRelation ()->getPivotAccessor ();
615
+
616
+ Gate::policy (Model::class, GreenPolicy::class);
617
+ Gate::policy (BelongsToManyRelation::class, GreenPolicy::class);
618
+
619
+ $ response = $ this ->post (
620
+ '/api/models/search ' ,
621
+ [
622
+ 'search ' => [
623
+ 'includes ' => [
624
+ [
625
+ 'relation ' => 'belongsToManyRelation ' ,
626
+ 'filters ' => [
627
+ ['field ' => 'models.pivot.number ' , 'operator ' => '= ' , 'value ' => null ]
628
+ ]
629
+ ],
630
+ ],
631
+ ],
632
+ ],
633
+ ['Accept ' => 'application/json ' ]
634
+ );
635
+
636
+
565
637
$ this ->assertResourcePaginated (
566
638
$ response ,
567
639
[$ matchingModel , $ matchingModel2 ],
0 commit comments