How to return multiple relationships with Laravel Eloquent?

Radical_Activity

I have a table called users. Each of these users has different things:

  • country
  • device
  • computer
  • category

I have created a table for each of these above 'things'. Similar the following:

1 | United States
2 | United Kingdom
3 | Australia
4 | Canada
5 | Italy

etc...

I'm storing these values in the users table as follows:

ID | Country | Device | Computer | Category |
---|---------|--------|----------|----------|
1  |       3 |      2 |        6 |        2 |
2  |       4 |      3 |        9 |        1 |

etc...

Now each of the above numbers are associated with the corresponding table's ID.

What I want is do an Eloquent Query and search for all the users and 'replacing' their corresponding values from their helper table.

I was thinking about doing a hasOne() Eloquent relationship for each of those things in the users table, but then I'm not sure how to get/call them at once.

Anyone can help me tacke this issue?

Andrew Nolan
$model = Model::with('relatedModel', 'relatedModelTwo')->get();

So in your case, it can be something like this. If you only want one relations returned with the user, remove the others.

$user = User::with('country', 'Device', 'Computer', 'Category')->get();

When you dd($user), you should see the related models in the relations array attribute.

To access the relations' properties from there, it is simply

$user->device->deviceAttribute

Edit For Comment:

Eloquent will assume the foreign key uses the Model name. So if your user table has the id column, it assumes there will be a column on device table called user_id. If you did this, then you are set and nothing should have to be done.

If you named your column to relate the models something else, you will need to pass that through as a second argument in the relation method.

public function device()
{
    return $this->hasOne('App\Device', 'foreign_key_here',);
}

Inversely, if you wanted to get the user the device belongs to, Eloquent will attempt to match the user_id column on the device table to the id column on the user table. As before, if you used a different column to related them, declare the foreign key as the second argument.

public function user()
{
    return $this->belongsTo('App\User', 'foreign_key_here',);
}

Laravel Doc One-To-One Relation

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel Eloquent querying relationships

From Dev

Laravel whereHas on multiple relationships

From Dev

Laravel: How to use multiple pivot table relationships

From Dev

Laravel Eloquent Relationships

From Dev

Laravel: how to filter eloquent data by relationships

From Dev

Laravel 5 Eloquent appending relationships to JSON on multiple levels

From Dev

Laravel eloquent relationships with foreign keys

From Dev

Laravel5: How are Eloquent model relationships expressed in the database?

From Dev

How to make Compact code Laravel 5 Eloquent Relationships

From Dev

Laravel 5.1 Eloquent with() and max() using multiple has-many relationships

From Dev

Creating instance of eloquent with multiple relationships?

From Dev

How to delete multiple records using Laravel Eloquent

From Dev

Laravel Eloquent Case Sensitive Relationships

From Dev

Laravel / Eloquent relationships & querying the database

From Dev

Laravel Eloquent: eager loading of multiple nested relationships

From Dev

Laravel 5.2 Eloquent - Finding all id's through multiple relationships

From Dev

Access eloquent relationships when return object as json

From Dev

Laravel eloquent complex queries on relationships

From Dev

Which relationship type to choose in Laravel Eloquent ORM on multiple relationships case

From Dev

Laravel: How to use multiple pivot table relationships

From Dev

Laravel Eloquent: Multiple Relationships between two Models, One-to-One AND One-to-Many

From Dev

How to make Compact code Laravel 5 Eloquent Relationships

From Dev

Drill down into laravel eloquent relationships

From Dev

Laravel 5.2 Nested Eloquent relationships

From Dev

Getting multiple relationships in Eloquent

From Dev

How to return multiple relationships with Laravel Eloquent?

From Dev

Eloquent, multiple relationships per table

From Dev

Return multiple instead of one in Laravel/Eloquent

From Dev

Laravel Eloquent: Bind Table to another table via pivot with multiple relationships

Related Related

  1. 1

    Laravel Eloquent querying relationships

  2. 2

    Laravel whereHas on multiple relationships

  3. 3

    Laravel: How to use multiple pivot table relationships

  4. 4

    Laravel Eloquent Relationships

  5. 5

    Laravel: how to filter eloquent data by relationships

  6. 6

    Laravel 5 Eloquent appending relationships to JSON on multiple levels

  7. 7

    Laravel eloquent relationships with foreign keys

  8. 8

    Laravel5: How are Eloquent model relationships expressed in the database?

  9. 9

    How to make Compact code Laravel 5 Eloquent Relationships

  10. 10

    Laravel 5.1 Eloquent with() and max() using multiple has-many relationships

  11. 11

    Creating instance of eloquent with multiple relationships?

  12. 12

    How to delete multiple records using Laravel Eloquent

  13. 13

    Laravel Eloquent Case Sensitive Relationships

  14. 14

    Laravel / Eloquent relationships & querying the database

  15. 15

    Laravel Eloquent: eager loading of multiple nested relationships

  16. 16

    Laravel 5.2 Eloquent - Finding all id's through multiple relationships

  17. 17

    Access eloquent relationships when return object as json

  18. 18

    Laravel eloquent complex queries on relationships

  19. 19

    Which relationship type to choose in Laravel Eloquent ORM on multiple relationships case

  20. 20

    Laravel: How to use multiple pivot table relationships

  21. 21

    Laravel Eloquent: Multiple Relationships between two Models, One-to-One AND One-to-Many

  22. 22

    How to make Compact code Laravel 5 Eloquent Relationships

  23. 23

    Drill down into laravel eloquent relationships

  24. 24

    Laravel 5.2 Nested Eloquent relationships

  25. 25

    Getting multiple relationships in Eloquent

  26. 26

    How to return multiple relationships with Laravel Eloquent?

  27. 27

    Eloquent, multiple relationships per table

  28. 28

    Return multiple instead of one in Laravel/Eloquent

  29. 29

    Laravel Eloquent: Bind Table to another table via pivot with multiple relationships

HotTag

Archive