@@ -521,4 +521,146 @@ public function test_getting_a_list_of_resources_including_belongs_to_many_relat
521
521
]
522
522
);
523
523
}
524
+
525
+ public function test_getting_a_list_of_resources_including_belongs_to_many_relation_and_filtering_on_pivot (): void
526
+ {
527
+ $ matchingModel = ModelFactory::new ()
528
+ ->hasAttached (
529
+ BelongsToManyRelationFactory::new ()->count (1 ),
530
+ ['number ' => 3 ],
531
+ 'belongsToManyRelation '
532
+ )
533
+ ->create ()->fresh ();
534
+
535
+ $ matchingModel2 = ModelFactory::new ()
536
+ ->hasAttached (
537
+ BelongsToManyRelationFactory::new ()->count (1 ),
538
+ ['number ' => 1 ],
539
+ 'belongsToManyRelation '
540
+ )
541
+ ->create ()->fresh ();
542
+
543
+ $ pivotAccessor = $ matchingModel ->belongsToManyRelation ()->getPivotAccessor ();
544
+
545
+ Gate::policy (Model::class, GreenPolicy::class);
546
+ Gate::policy (BelongsToManyRelation::class, GreenPolicy::class);
547
+
548
+ $ response = $ this ->post (
549
+ '/api/models/search ' ,
550
+ [
551
+ 'search ' => [
552
+ 'includes ' => [
553
+ [
554
+ 'relation ' => 'belongsToManyRelation ' ,
555
+ 'filters ' => [
556
+ ['field ' => 'models.pivot.number ' , 'operator ' => '> ' , 'value ' => 2 ],
557
+ ],
558
+ ],
559
+ ],
560
+ ],
561
+ ],
562
+ ['Accept ' => 'application/json ' ]
563
+ );
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
+ $ this ->assertResourcePaginated (
637
+ $ response ,
638
+ [$ matchingModel , $ matchingModel2 ],
639
+ new ModelResource (),
640
+ [
641
+ [
642
+ 'belongs_to_many_relation ' => $ matchingModel ->belongsToManyRelation ()
643
+ ->orderBy ('id ' , 'desc ' )
644
+ ->get ()
645
+ ->map (function ($ relation ) use ($ pivotAccessor ) {
646
+ return collect ($ relation ->only (
647
+ array_merge ((new BelongsToManyResource ())->getFields (app ()->make (RestRequest::class)), [$ pivotAccessor ])
648
+ ))
649
+ ->pipe (function ($ relation ) use ($ pivotAccessor ) {
650
+ $ relation [$ pivotAccessor ] = collect ($ relation [$ pivotAccessor ]->toArray ())
651
+ ->only (
652
+ (new ModelResource ())->relation ('belongsToManyRelation ' )->getPivotFields ()
653
+ );
654
+
655
+ return $ relation ;
656
+ });
657
+ })
658
+ ->toArray (),
659
+ ],
660
+ [
661
+ 'belongs_to_many_relation ' => [],
662
+ ],
663
+ ]
664
+ );
665
+ }
524
666
}
0 commit comments