'Laravel Relationships, don't know how to display

I have two models. Business and City.

Business:

  1. id
  2. title

-some columns--

  1. city_id

City:

  1. id
  2. name

How to display the city name, when I get business data to view

I was able to display cities using the laravel voyager lessons enter image description here

enter image description here

When I want to get it like $business->city_id enter image description here



Solution 1:[1]

You have 2 options. First, you can get city details on the controller:

Business::with('city')...;

On the blade

$business->city->name;

Other option fetching it on the blade directly. I don't recommend this because of extra database queries.

$business->city()->name;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bulent