Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Verse's avatar

Relationship between three tables

I have three databases business, photos and subscriptions. Need to display results from business table when user is subscribed. Made relationship between them but have not managed to make it work. Payment is made trough Braintree. How to display results from business table, now just subscription is shown.

Business.php

public function subscription()
    {
        return $this->belongsTo('App\Subscription');
    }

Subscription.php

public function business ()
    {
        return $this->hasMany('App\Business ');
    }

PagesController.php

public function home()
    {
        $business = Subscription::where('name', 'main')->get();
        return view('pages.home', compact('business '));
    }

home.php

@foreach($business as $bussiness)
    <tr class="view">
        <td>@include('partials.gallery')</td>
        <td><b class="text">{{ $business ->business_name }}</b>
            {{ substr($business ->description, 0, 60) }}...<br>
            <a href="{!! route('business.show', ['business ' => $business ->id]) !!}"
               role="button">
            </a>
        </td>
    </tr>
@endforeach

Tried @foreach($business->business as $bussiness) but then I get Undefined property: Illuminate\Database\Eloquent\Collection::$business

0 likes
7 replies
bestmomo's avatar

I think Business belongsTo Subscription :

$business = Business::whereHas('subscription', function ($query) {
    $query->whereName('main');
})->get();
yassiNebeL's avatar

Two databases or Two Tables ?

You need to declare the inverse relationship in Business model, and load your query like that :

Subscription::where('name', 'main')->with('business')->get()

1 like
Verse's avatar

@bestmomo @yassiNebeL Getting this error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'business.subscription_id' in 'where clause' 
(SQL: select * from `business` where exists (select * from `business` 
where `business`.`subscription_id` = `business`.`id` and `name` = main))

Do I need to make connection on user_id between organization and subscriptions?

yassiNebeL's avatar

Which relatonship did you use? what's the structure of business table?

the error says that subscription_id is not found on your table business.

yassiNebeL's avatar

Did you add subscription_id on business table ?

let you query like that Subscription::where('name', 'main')->with('business')->get() and try this : $business->subscription in your view of course using foreach.

I suggest you php artisan tinkerthis how I test my relationships.

Verse's avatar

Yes, I added subscription_id, tried it, getting Undefined property: Illuminate\Database\Eloquent\Collection::$subscription

Verse's avatar
Verse
OP
Best Answer
Level 1

It's working, this is how I did it

Business

public function showBusiness()
    {
        return $this->hasOne('App\Subscription');
    }

Subscription

public function businessSubscribed()
    {
        return $this->belongsTo('App\Business ', 'user_id', 'user_id');
    }

public function hasPhotos()
    {
        return $this->hasManyThrough('App\Photo', 'App\Business', 'user_id', 'business_id');
    }

Photo

public function photoSubscribed()
    {
        return $this->hasManyThrough('App\Subscription', 'App\Business');
    }

home.php

@foreach(App\Subscription::where('name', 'main')->with('businessSubscribed')->get() as $business)
    <tr class="view">
        @if(count($business->hasPhotos ))
             <img src="{{ url($business->hasPhotos[0]->thumbnail_path) }}"
         class="img-thumbnail img-responsive">
      @endif
        <td><b class="text">{{ $business ->businessSubscribed->business_name }}</b>
            {{ substr($business->businessSubscribed->description, 0, 60) }}...<br>
            <a href="{!! route('business.show', ['business ' => $business ->businessSubscribed->id]) !!}"
               role="button">
            </a>
        </td>
    </tr>
@endforeach

Please or to participate in this conversation.