How to grab data from another table with this relationship setup?

Andy

I have four tables

City

  id
  city
  state_id

State

 id
 name

City_Vendor

 id
 vendor_id
 city_id

Vendor

 id 
 name

Now if I do something like this I get the cities and the state_id but not the state name

   Vendor::with('cities')->get();

This is what I did to get the states in my view which is not ideal..

              <?php foreach($vendor['cities'] as $state): ?>
                  @if($state['state_id'] == $city['state_id'])
                    {{$state['state']['state']}}
                    <?php break; ?>
                  @endif
              <?php endforeach;?>

I would like to get states name too not just the state_id. What would would be the best way to go about doing this?

Marcin Nabiałek

Assuming you have correct relationship, it should be rather something like that:

@foreach ($vendor->cities as $city)
  {{ $vendor->city->city}}, {{$vendor->city->state->name}}
@endforeach

In above code you display for one vendor city with state name

and instead of:

Vendor::with('cities')->get();

you should rather use:

Vendor::with('cities.state')->get();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to grab data from table with divs and inputs inside cells?

From Dev

how to set autocomplete to show data from another table in laravel (OneToMany Relationship)

From Dev

How to data in a table from another table?

From Dev

Grab Data on the Fly from Table HTML

From Dev

How to grab draggable data and append to another div

From Dev

How to grab draggable data and append to another div

From Dev

How to grab data from Json data in API

From Dev

Laravel eloquent model how to get data from relationship's table

From Dev

How to SELECT from a table if no data exists in another

From Dev

How to count data from another table

From Dev

how to insert data from another table with id from my table

From Dev

Grab value from another table via foreign key

From Dev

Select value when no relationship from another table

From Dev

Select value when no relationship from another table

From Dev

Getting id from another table in a relationship laravel

From Dev

How to grab data from multiple field pairs

From Dev

Is it possible to get data from the relationship table of a relationship table in Laravel?

From Dev

Obtaining a count of data from one SQL table with a relationship to another and using this in java

From Dev

Grab from table values

From Dev

How to select data from a table and insert into another table?

From Dev

How to Replace and Update Data From One Table to Another Table in MySQL

From Dev

How to transfer data from one table of a database to a table in another database

From Dev

How do i copy data from one table to another table?

From Dev

How to select data from a table and insert into another table?

From Dev

How to Replace and Update Data From One Table to Another Table in MySQL

From Dev

How to create a dynamic table from another table's data in SQL

From Dev

How to transfer data from one table of a database to a table in another database

From Dev

How to select data from a table by referring to another table in MySQL

From Dev

How do i copy data from one table to another table?

Related Related

  1. 1

    How to grab data from table with divs and inputs inside cells?

  2. 2

    how to set autocomplete to show data from another table in laravel (OneToMany Relationship)

  3. 3

    How to data in a table from another table?

  4. 4

    Grab Data on the Fly from Table HTML

  5. 5

    How to grab draggable data and append to another div

  6. 6

    How to grab draggable data and append to another div

  7. 7

    How to grab data from Json data in API

  8. 8

    Laravel eloquent model how to get data from relationship's table

  9. 9

    How to SELECT from a table if no data exists in another

  10. 10

    How to count data from another table

  11. 11

    how to insert data from another table with id from my table

  12. 12

    Grab value from another table via foreign key

  13. 13

    Select value when no relationship from another table

  14. 14

    Select value when no relationship from another table

  15. 15

    Getting id from another table in a relationship laravel

  16. 16

    How to grab data from multiple field pairs

  17. 17

    Is it possible to get data from the relationship table of a relationship table in Laravel?

  18. 18

    Obtaining a count of data from one SQL table with a relationship to another and using this in java

  19. 19

    Grab from table values

  20. 20

    How to select data from a table and insert into another table?

  21. 21

    How to Replace and Update Data From One Table to Another Table in MySQL

  22. 22

    How to transfer data from one table of a database to a table in another database

  23. 23

    How do i copy data from one table to another table?

  24. 24

    How to select data from a table and insert into another table?

  25. 25

    How to Replace and Update Data From One Table to Another Table in MySQL

  26. 26

    How to create a dynamic table from another table's data in SQL

  27. 27

    How to transfer data from one table of a database to a table in another database

  28. 28

    How to select data from a table by referring to another table in MySQL

  29. 29

    How do i copy data from one table to another table?

HotTag

Archive