@@ -491,6 +491,78 @@ public function test_getting_a_list_of_resources_including_belongs_to_many_relat
491491 ['Accept ' => 'application/json ' ]
492492 );
493493
494+ $ this ->assertResourcePaginated (
495+ $ response ,
496+ [$ matchingModel , $ matchingModel2 ],
497+ new ModelResource (),
498+ [
499+ [
500+ 'belongs_to_many_relation ' => $ matchingModel ->belongsToManyRelation ()
501+ ->orderBy ('id ' , 'desc ' )
502+ ->get ()
503+ ->map (function ($ relation ) use ($ pivotAccessor ) {
504+ return collect ($ relation ->only (
505+ array_merge ((new BelongsToManyResource ())->getFields (app ()->make (RestRequest::class)), [$ pivotAccessor ])
506+ ))
507+ ->pipe (function ($ relation ) use ($ pivotAccessor ) {
508+ $ relation [$ pivotAccessor ] = collect ($ relation [$ pivotAccessor ]->toArray ())
509+ ->only (
510+ (new ModelResource ())->relation ('belongsToManyRelation ' )->getPivotFields ()
511+ );
512+
513+ return $ relation ;
514+ });
515+ })
516+ ->toArray (),
517+ ],
518+ [
519+ 'belongs_to_many_relation ' => [],
520+ ],
521+ ]
522+ );
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+
494566 $ this ->assertResourcePaginated (
495567 $ response ,
496568 [$ matchingModel , $ matchingModel2 ],
0 commit comments