Files
investbrain/app/Support/Spotlight.php
T

45 lines
1.3 KiB
PHP
Raw Normal View History

2024-08-01 13:53:10 -05:00
<?php
namespace App\Support;
2024-08-23 22:49:08 -05:00
use App\Models\Holding;
2024-08-08 22:16:23 -05:00
use App\Models\Portfolio;
2024-08-01 13:53:10 -05:00
use Illuminate\Http\Request;
class Spotlight
{
public function search(Request $request)
{
2024-08-08 22:16:23 -05:00
2024-08-23 22:49:08 -05:00
$results = collect();
2024-08-08 22:16:23 -05:00
if (!$request->user()) {
2024-08-01 13:53:10 -05:00
2024-08-23 22:49:08 -05:00
return $results;
}
2024-08-08 22:16:23 -05:00
2024-08-27 22:06:10 -05:00
$portfolios = $request->user()->portfolios()->where('title', 'LIKE', '%'.$request->input('search').'%')->limit(5)->get();
2024-08-23 22:49:08 -05:00
$portfolios->each(function($portfolio) use ($results) {
2024-08-08 22:16:23 -05:00
2024-08-23 22:49:08 -05:00
$results->push([
'name' => 'Portfolio: '. $portfolio->title,
2024-08-08 22:16:23 -05:00
'description' => null,
'link' => route('portfolio.show', ['portfolio' => $portfolio->id]),
'avatar' => null
2024-08-23 22:49:08 -05:00
]);
});
2024-08-27 22:06:10 -05:00
$holdings = $request->user()->holdings()->where('holdings.symbol', 'LIKE', '%'.$request->input('search').'%')->limit(5)->get();
2024-08-23 22:49:08 -05:00
$holdings->each(function($holding) use ($results) {
$results->push([
'name' => 'Holding: '. $holding->symbol,
'description' => $holding->portfolio->title,
'link' => route('portfolio.show', ['portfolio' => $holding->portfolio->id]),
'avatar' => null
]);
2024-08-08 22:16:23 -05:00
});
2024-08-23 22:49:08 -05:00
return $results;
2024-08-01 13:53:10 -05:00
}
}