Summerize what? Count?
$count = $inBusinesses->count() + $outBusinesses->count();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
How can these two variables be summarized? And put in a line?
public function index()
{
$inBusinesses = Business::query()->where('options', 0)->latest()->take(3)->get();
$outBusinesses = Business::query()->where('options', 1)->latest()->take(3)->get();
return view('Home.index', compact('inBusinesses', 'outBusinesses'));
}
Summerize what? Count?
$count = $inBusinesses->count() + $outBusinesses->count();
@Sinnbeck No, The $inBusinesses and $outBusinesses.
@kaphsh so two variables = 2? Or employees? Or income? Or..?
@Sinnbeck I have two tabs in my blade Out Businesses and In Businesses
<nav>
<div class="nav nav-tabs nav-pills" id="nav-tab" role="tablist">
<button class="nav-link btn-lg active rounded-0" id="nav-internal-tab" data-bs-toggle="tab" data-bs-target="#nav-internal" type="button" role="tab" aria-controls="nav-internal" aria-selected="true">In Businesses</button>
<button class="nav-link btn-lg rounded-0" id="nav-foreign-tab" data-bs-toggle="tab" data-bs-target="#nav-foreign" type="button" role="tab" aria-controls="nav-foreign" aria-selected="false">Out Businesses</button>
</div>
</nav>
@kaphsh are you wanting to get the results for both classifications using a single query?
@kaphsh yes? So you need to show each variable inside a div?
@Sinnbeck Yes
@tykus Yes, I am wanting to get the results for both classifications using a single query.
@kaphsh so something like this?
<div>
@foreach ($inBusinesses as $business)
//render the content of each
@endforeach
</div>
<div>
@foreach ($outBusinesses as $business)
//render the content of each
@endforeach
</div>
Can options ever be anything but 0 and 1? And why combine them? Seems like quite a tiny optimization, especially if you are going to split them with php afterwards
@kaphsh You can use a union for that.
There is an example here https://tray2.se/posts/database-design-part-2
books = DB::table('books')
->select('id', 'title', 'created_at', DB::raw("'books' AS source"));
$movies = DB::table('movies')
->select('id', 'title', 'created_at', DB::raw("'movies' AS source"));
$latestTen = DB::table('records')
->select('id', 'title', 'created_at', DB::raw("'movies' AS source"));
->unionAll($books)
->unionAll($movies)
->orderBy('created_at', 'desc')
->limit(10)
->get();
@Sinnbeck Ok, This is what I know.
My problem with the controller is that I want to put these two variables in a line.
I tried to change this line but, it is not working
$businesses = Business::query()->where('options', 1 + 1)->latest()->take(3)->get();
@kaphsh so you need a window function; but AFAIK Eloquent doesn't support this. Why are two queries a problem?
@kaphsh a window function; might look like this:
SELECT * FROM (
SELECT businesses.*, ROW_NUMBER() OVER (PARTITION BY options ORDER BY created_at DESC) as row
FROM businesses
) as t1
WHERE t1.row <= 3
But, as mentioned earlier, unsupported by Eloquent.
@Sinnbeck Can these two variables be removed and summarized as follows?
public function index()
{
$businesses = Business::query()->where('options', 1 + 1)->latest()->take(3)->get();
return view('Home.index', compact('businesses'));
}
@kaphsh Yes with a union.
@Tray2 How?
@kaphsh here is a simple version that does two queries but no temp variables
$businesses = Business::query()->where('options', 0)->latest()->take(3)->get()->merge(Business::query()->where('options', 1)->latest()->take(3)->get());
@kaphsh If you read the post I linked it tells you how.
@Sinnbeck How to Display in the tab of blade
<div class="tab-content pt-4" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-internal" role="tabpanel" aria-labelledby="nav-internal-tab">
<div class="row">
@foreach($investments as $investment)
<div class="col-lg-4 mb-3">
<div class="card bg-transparent border-0">
<div class="card-body">
<p><i class="{{ $investment->icon }}"></i></p>
<a href="/investment/{{ $investment->id }}" class="h5">{{ $investment->title }}</a>
<p class="card-text">{{ $investment->text }}</p>
</div>
</div>
</div>
@endforeach
</div>
</div>
<div class="tab-pane fade" id="nav-foreign" role="tabpanel" aria-labelledby="nav-foreign-tab">
<div class="row">
@foreach($investments as $investment)
<div class="col-lg-4 mb-3">
<div class="card bg-transparent border-0">
<div class="card-body">
<p><i class="{{ $investment->icon }}"></i></p>
<h5 class="card-title">
<a href="{{ $investment->id }}">{{ $investment->title }}</a>
</h5>
<p class="card-text">{{ $investment->text }}</p>
</div>
</div>
</div>
@endforeach
</div>
</div>
</div>
@kaphsh you can either group or partition the Collection using the options property on each Model instance.
@kaphsh do each type to be inside seperate tabs or in the same? I have a code example some answers back
@Sinnbeck How?
@kaphsh look at my previous post. And why is your example $investments? Should that be businesses?
I am still unsure what you need the end result to be. It sounds like you want to merge it into 1 variable, only to split it up again in the view?
@Sinnbeck Not. It is generally with "$ investments".
@kaphsh I have no idea what that means. Maybe it is time you spend just a couple of minutes explaining exactly what you want
My problem with the controller is that I want to put these two variables in a line.
The $inBusinesses and $outBusinesses.
My Controller
public function index()
{
$investments = Investment::query()->where('options', 0)->latest()->take(3)->merge(Investment::query()->where('options', 1)->latest()->take(3)->get());
return view('Home.index', compact('investments'));
}
index.blade.php
<nav>
<div class="nav nav-tabs nav-pills" id="nav-tab" role="tablist">
<button class="nav-link btn-lg active rounded-0" id="nav-internal-tab" data-bs-toggle="tab" data-bs-target="#nav-internal" type="button" role="tab" aria-controls="nav-internal" aria-selected="true">internal</button>
<button class="nav-link btn-lg rounded-0" id="nav-foreign-tab" data-bs-toggle="tab" data-bs-target="#nav-foreign" type="button" role="tab" aria-controls="nav-foreign" aria-selected="false">foreign</button>
</div>
</nav>
<div class="tab-content pt-4" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-internal" role="tabpanel" aria-labelledby="nav-internal-tab">
<div class="row">
@foreach($investments as $investment)
<div class="col-lg-4 mb-3">
<div class="card bg-transparent border-0">
<div class="card-body">
<p><i class="{{ $investment->icon }}"></i></p>
<a href="/investment/{{ $investment->id }}" class="h5">{{ $investment->title }}</a>
<p class="card-text">{{ $investment->text }}</p>
</div>
</div>
</div>
@endforeach
</div>
</div>
<div class="tab-pane fade" id="nav-foreign" role="tabpanel" aria-labelledby="nav-foreign-tab">
<div class="row">
@foreach($investments as $investment)
<div class="col-lg-4 mb-3">
<div class="card bg-transparent border-0">
<div class="card-body">
<p><i class="{{ $investment->icon }}"></i></p>
<h5 class="card-title">
<a href="{{ $investment->id }}">{{ $investment->title }}</a>
</h5>
<p class="card-text">{{ $investment->text }}</p>
</div>
</div>
</div>
@endforeach
</div>
</div>
</div>
@kaphsh so make two queries as in your original post and have a foreach for each. Keep it simple
@Sinnbeck Ok, How?
@kaphsh something like this. Just named them 1 and 2 for simplicity, and because I don't get why your variables change names
<nav>
<div class="nav nav-tabs nav-pills" id="nav-tab" role="tablist">
<button class="nav-link btn-lg active rounded-0" id="nav-internal-tab" data-bs-toggle="tab" data-bs-target="#nav-internal" type="button" role="tab" aria-controls="nav-internal" aria-selected="true">internal</button>
<button class="nav-link btn-lg rounded-0" id="nav-foreign-tab" data-bs-toggle="tab" data-bs-target="#nav-foreign" type="button" role="tab" aria-controls="nav-foreign" aria-selected="false">foreign</button>
</div>
</nav>
<div class="tab-content pt-4" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-internal" role="tabpanel" aria-labelledby="nav-internal-tab">
<div class="row">
@foreach($investments1 as $investment)
<div class="col-lg-4 mb-3">
<div class="card bg-transparent border-0">
<div class="card-body">
<p><i class="{{ $investment->icon }}"></i></p>
<a href="/investment/{{ $investment->id }}" class="h5">{{ $investment->title }}</a>
<p class="card-text">{{ $investment->text }}</p>
</div>
</div>
</div>
@endforeach
</div>
</div>
<div class="tab-pane fade" id="nav-foreign" role="tabpanel" aria-labelledby="nav-foreign-tab">
<div class="row">
@foreach($investments2 as $investment)
<div class="col-lg-4 mb-3">
<div class="card bg-transparent border-0">
<div class="card-body">
<p><i class="{{ $investment->icon }}"></i></p>
<h5 class="card-title">
<a href="{{ $investment->id }}">{{ $investment->title }}</a>
</h5>
<p class="card-text">{{ $investment->text }}</p>
</div>
</div>
</div>
@endforeach
</div>
</div>
</div>
The controller has an error.
Call to undefined method Illuminate\Database\Eloquent\Builder::merge()
@kaphsh drop the merge. Just have two variables
But I fixed the merge in my post
Please or to participate in this conversation.