Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

StackOverflow Point

StackOverflow Point Navigation

  • Web Stories
  • Badges
  • Tags
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Web Stories
  • Badges
  • Tags
Home/ Questions/Q 185944
Alex Hales
  • 0
Alex HalesTeacher
Asked: June 10, 20222022-06-10T12:03:25+00:00 2022-06-10T12:03:25+00:00

php – How do I pipe an Eloquent HasManyThrough through another 1:1 relationship

  • 0

[ad_1]

A simplified version of my schema looks like this:

Events

Bookings

Participants

  • participant_id
  • booking_id

Users

Users are booked onto events via participant records, which belong to booking records, which are children of event records.

I am trying to write a method on the Event model to return all the users on the event, as an Eloquent relationship. I want it as Eloquent so I can chain where clauses etc., to further filter the resultset.

I have a participants() method on the Event model, that looks like this:

public function participants(): \Illuminate\Database\Eloquent\Relations\HasManyThrough
{
    return $this->hasManyThrough(
        Participant::class,
        Booking::class,
        'event_id',
        'booking_id',
        'participant_id',
        'booking_id'
    );
}

This works, and returns a HasManyThrough that I can traverse, or work with further.

My problem is in trying to create a users() method, without starting a new query builder.
I want to chain something to the participants() method, to get the User attached to each participant, and return this collection as an Eloquent relationship.

I realise I can do the following, but I end up with a simple array of objects this way, rather than a queryable Eloquent relationship. Which means I’ve lost the ORM functionality I want.

public function users()
{
    $users = [];
    foreach($this->participants as $participant){
        $users[] = $participant->user;
    }
    return $users;
}

How would I go about turning my participants collection into users? Is it possible to build Eloquent relationships (or collections) ‘by hand’?

[ad_2]

  • 0 0 Answers
  • 4 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report
Leave an answer

Leave an answer
Cancel reply

Browse

Sidebar

Ask A Question

Related Questions

  • xcode - Can you build dynamic libraries for iOS and ...

    • 0 Answers
  • bash - How to check if a process id (PID) ...

    • 396 Answers
  • database - Oracle: Changing VARCHAR2 column to CLOB

    • 369 Answers
  • What's the difference between HEAD, working tree and index, in ...

    • 361 Answers
  • Amazon EC2 Free tier - how many instances can I ...

    • 0 Answers

Stats

  • Questions : 43k

Subscribe

Login

Forgot Password?

Footer

Follow

© 2022 Stackoverflow Point. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.