I think Business belongsTo Subscription :
$business = Business::whereHas('subscription', function ($query) {
$query->whereName('main');
})->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
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.