@tykus Ok, ill explain better. All the things ive been writing are coming from the multiple questions on how to manage things in Laravel. Im new to this framework, and i need to understand things better.
The main problem came from this development i was doing.
Im creating an application where i need to search on a map accounts living on that range of coordinates and other filters by ajax and i need to call this function from multiple places.
So i created a SearchController
class SearchController extends Controller
{
public function index(Request $request)
{
$pageNum = (isset($request['pageNum']) && $request['pageNum'] > 0) ? (int)$request['pageNum'] : 1;
$accountTypeConst = (isset($request['search_type']) && in_array($request['search_type'], [1, 2])) ? $request['search_type'] : NULL;
$limit = 10;
$tmpParams = [
'accountTypeConst' => $accountTypeConst,
'tab' => 'list',
'limit' => $limit,
'page' => $pageNum,
'lat' => isset($params['lat']) ? $params['lat'] : 0,
'lng' => isset($params['lng']) ? $params['lng'] : 0,
'neLat' => isset($params['neLat']) ? $params['neLat'] : 0,
'neLng' => isset($params['neLng']) ? $params['neLng'] : 0,
'swLat' => isset($params['swLat']) ? $params['swLat'] : 0,
'swLng' => isset($params['swLng']) ? $params['swLng'] : 0,
'center' => isset($params['center']) ? $params['center'] : '0,0'
];
$items = Account::search($tmpParams);
and also i created the search function inside the Account model to return the account items i found after doing some data manipulation.
class Account extends Model
{
use HasFactory;
public function search($params = array())
{
$query = Account::with(['brands', 'brandsspecialize', 'country', 'province'])
->select(
'accounts.*',
'services.price AS service_price'
)
->join('countries', 'countries.id', 'accounts.country_id')
->join('provinces', 'provinces.id', 'accounts.province_id')
...
$accounts = $query->paginate($limit);
$total = $accounts->total();
$items = array();
foreach ($accounts as $account) :
$brands = $account->Brands;
$brand_list = [];
//dump($brands);
foreach ($brands as $brand) {
if ($brand->id == 1) {
$brand_list[] = 'Tutti i marchi';
break;
}
$brand_list[] = $brand->description;
}
$brandsSpecialize = $account->BrandsSpecialize;
$brand_specialize_list = [];
foreach ($brandsSpecialize as $brand) {
if ($brand->id == 1) {
$brand_specialize_list[] = 'Tutti i marchi';
break;
}
$brand_specialize_list[] = $brand->description;
}
$item = [
'id' => $account->id,
'marker_class' => '',
'img_alt' => $account->name,
.....
......
'is_liked' => $account->is_favorite, //From query //left join con user_favorite
];
$items[] = $item;
endforeach;
return $items;
}
I also want to return the total.
I was thinking putting the total in some variable and fetching that from the controller. But i believe the solution is to return an array of ['items' => $items, 'total' => $total] right?