How to eager load only 1 result from many to many relationship Laravel

SymmetricsWeb

How can I eager load only one from many to many relationship?

I have two models Application & Applicant

Models\Application.php

public function applicants() {
    return $this->belongsToMany(Applicant::class);
}

public function oneApplicant() {
    return $this->applicants()->first();
}

I'm wanted to paginate on the applications and want to load only one applicant (if they have.)

return Application::stage( $stages )
->stagenot($stageNot)
->refered( $refered, $term )
->with(['oneApplicant'])
->orderBy('created_at','desc')->paginate( $count );

But it isn't working. Getting the first result like this return $this->applicants()->first() will produce an error Call to undefined method Illuminate\Database\Query\Builder::with()

If I also put a limit instead of first return $this->applicants()->limit(1) it will only show one applicant to the last collection.

I also tried to modify the query directly on eager loading call to get the first row

return Application::with(['applicants',  => function( $query ) {
        $q->first();
    }])->orderBy('created_at','desc')->paginate( $count );

But the result is the same as adding a limit(1) on directly on the relation, it will only add one applicant to the last item from collection and the other item have empty array value

Can anybody help :)

Thanks

SymmetricsWeb

I realized its too complex to achieve it myself and ended up using the eloquent-eager-limit package from staudenmeir (big thanks to him, save me hours of work :) )

here's my model

class Application extends BaseModel {
    use \Staudenmeir\EloquentEagerLimit\HasEagerLimit;

    public function applicants() {
        return $this->belongsToMany(Applicant::class);
    }

    public function oneApplicant() {
        return $this->applicants()->limit(1);
    }
}

Then I was able to use it on my controller

return Application::stage( $stages )
    ->stagenot($stageNot)
    ->refered( $refered, $term )
    ->with([
        'oneApplicant',
        'oneApplicant.onePhone:model_id,number',
        'oneApplicant.oneAddress:model_id,state'
    ])->orderBy('created_at','desc')->paginate( $count );

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Laravel Eloquent Eager Loading Multiple Has Many Relationship

分類Dev

Laravel fetch only pivot columns in many to many relationship

分類Dev

View Many to Many Relationship in laravel

分類Dev

How to insert a one to many relationship in Laravel

分類Dev

Laravel save / update many to many relationship

分類Dev

Friendship system with Laravel : Many to Many relationship

分類Dev

Laravel One to Many of One to Many relationship

分類Dev

How to handle Many to Many relationship in mongoDB?

分類Dev

How to fetch data from one to many relationship using Django queries

分類Dev

Laravel, pivot table conflicts with many-to-many relationship

分類Dev

Laravel 5.5 Update a checkbox of many to many relationship model

分類Dev

Laravel: Add a join to self referencing Many-to-Many relationship?

分類Dev

Synchronizing a one-to-many relationship in Laravel

分類Dev

Laravel 5 Eloquent Relationship - Has Many Though

分類Dev

EF Core 3.1 how to automap in Many-to-many relationship

分類Dev

SQL Server : how can I filter a many to many relationship with delimiter

分類Dev

Many to many relationship CRUD in Room

分類Dev

Many-to-many relationship as list

分類Dev

backward filter and many to many relationship

分類Dev

Power Pivot Many to Many Relationship

分類Dev

Querying a many to many relationship in SQL

分類Dev

How to map info from the relationship table from the many-to-many relatonship table into entity model class using AutoMapper

分類Dev

Realm : How to structure one to many relationship

分類Dev

Access result from first select in Dapper QueryMultiple (many to many)

分類Dev

How to concatenate data from a many to many table?

分類Dev

Laravel Eloquent: In many to many relationships the ORM is returning only one Object

分類Dev

How to migrate a existing one-to-many relationship to many-to-many in Rails and ActiveRecord

分類Dev

Convert existing many to many to reverse many to many relationship using XSLT

分類Dev

Creating one to many relationship with pivot table of many to many relationship

Related 関連記事

  1. 1

    Laravel Eloquent Eager Loading Multiple Has Many Relationship

  2. 2

    Laravel fetch only pivot columns in many to many relationship

  3. 3

    View Many to Many Relationship in laravel

  4. 4

    How to insert a one to many relationship in Laravel

  5. 5

    Laravel save / update many to many relationship

  6. 6

    Friendship system with Laravel : Many to Many relationship

  7. 7

    Laravel One to Many of One to Many relationship

  8. 8

    How to handle Many to Many relationship in mongoDB?

  9. 9

    How to fetch data from one to many relationship using Django queries

  10. 10

    Laravel, pivot table conflicts with many-to-many relationship

  11. 11

    Laravel 5.5 Update a checkbox of many to many relationship model

  12. 12

    Laravel: Add a join to self referencing Many-to-Many relationship?

  13. 13

    Synchronizing a one-to-many relationship in Laravel

  14. 14

    Laravel 5 Eloquent Relationship - Has Many Though

  15. 15

    EF Core 3.1 how to automap in Many-to-many relationship

  16. 16

    SQL Server : how can I filter a many to many relationship with delimiter

  17. 17

    Many to many relationship CRUD in Room

  18. 18

    Many-to-many relationship as list

  19. 19

    backward filter and many to many relationship

  20. 20

    Power Pivot Many to Many Relationship

  21. 21

    Querying a many to many relationship in SQL

  22. 22

    How to map info from the relationship table from the many-to-many relatonship table into entity model class using AutoMapper

  23. 23

    Realm : How to structure one to many relationship

  24. 24

    Access result from first select in Dapper QueryMultiple (many to many)

  25. 25

    How to concatenate data from a many to many table?

  26. 26

    Laravel Eloquent: In many to many relationships the ORM is returning only one Object

  27. 27

    How to migrate a existing one-to-many relationship to many-to-many in Rails and ActiveRecord

  28. 28

    Convert existing many to many to reverse many to many relationship using XSLT

  29. 29

    Creating one to many relationship with pivot table of many to many relationship

ホットタグ

アーカイブ