It's been three days that I block on my configuration. I can not sort by model reported, I tried many methods but nothing conclusive, for example, sortBy / sortByDesc, join table but no solution
I want to perform paging, sorting, and server-side filters. Filters can be multiple. Everything works except sort by the related model I get the URL: http://127.0.0.1:8000/api/user/list?page=1&limit=30&sort=user__created_at&direction=desc&search__user__name=James&search__group__name=Client
the query search__model__col
allows me to chain several searches on several models or columns
Here is my code :
public static function pagination(Request $request, $model, $relation, $modelStringify)
{
$modelSortBy = explode('__', $request->query('sort'))[0]; // user
$tableSortBy = explode('__', $request->query('sort'))[1]; // created_at
$query = $request->query(); // ['page' => 1, 'limit' => 30...]
$direction = $request->query('direction'); // desc
try {
$model = $model->with($relation);
//TODO : orderBy relationship
if ($modelSortBy === $modelStringify): // modelStringify = 'user'
$model->orderBy($tableSortBy, $direction);
else:
// sort server side here
endif;
foreach (array_keys($query) as $q) {
if (strpos($q, 'search__') === 0) {
$modelSearchBy = explode('__', $q)[1]; // ['user', 'group']
$tableSearchBy = explode('__', $q)[2]; // ['name', 'name']
$valueSearchBy = $query[$q];
if ($modelSearchBy === $modelStringify):
$model->where($tableSearchBy, 'like', $valueSearchBy . '%');
else:
$model->whereHas($modelSearchBy, function (Builder $q) use ($valueSearchBy, $tableSearchBy) {
$q->where($tableSearchBy, 'like', $valueSearchBy . '%'); // $valueSearchBy = ['James', 'Client']
});
endif;
}
}
return $model;
} catch (Exception $e) {
return response()->json(['message' => $e], 400);
}
}
Here is an example for user
belongsTo
group
:
return response()->json([
'items' => Utils::pagination($request, new User(), [
'group',
], 'user')->paginate($limit)->toArray()
]);
How can I introduce the sort by the related model in this configuration?
For example, for sorted by group name, I get the URL &sort=group__name
Thank you
from Laravel: how to sort by related model with pagination & filter
The majority of foreign citizens who wish to enjoy a stay in the Russian Federation are required to apply for a eVisa for Russia, although the type of travel document required will depend on nationality, as well as the purpose and duration of the stay. Tourists intending to visit Russia for a short term stay can now apply for Russia eVisas, a much faster and more practical way to obtain a Russian travel authorization than the traditional Russian tourist visa.
ReplyDeleteeVisas allow visitors to apply for their Russian visa entirely online in minutes, from.
anywhere in the world. Passport copies, online application form, and other documents required for the visa can be submitted online. Currently, eVisas are available for specific Russian regions. In order to visit the regions of the Far East, Kaliningrad, and St Petersburg, it’s no longer necessary to present supporting documentation at a Russian embassy or consulate.
E-visas can be of the following categories only: ordinary business visa,ordinary tourist visa, and ordinary humanitarian visa. If the purpose of your journey to the Russian Federation does not correspond to any of the above, you should apply for a traditional visa at a diplomatic mission or consular office of the Russian Federation