简体   繁体   English

Laravel Orderby中面临的问题

[英]Facing issue in laravel orderby

I am facing a issue with laravel order by function. 我正面临laravel按功能排序的问题。 i want to fetch the records according to country and the result should start from the current user's country. 我想根据国家/地区获取记录,结果应从当前用户的国家/地区开始。

For example. 例如。 if auth user belong to india then list should start from india. 如果auth用户属于印度,则列表应从印度开始。

My Current code is 我当前的代码是

$users = User::where('status', '=',1)->with(['profile'])->latest()->orderBy('last_login', 'desc')->paginate(12);

Thanks 谢谢

Ok, whatever I've understood, you are looking to sort the data through the country attributes from profiles table, which is in relation with User model, for this you need to use join I'm not sure about your structure so I'm just giving you an example. 好吧,不管我的理解,你正在寻找的数据通过整理country从属性profiles表,这是在与有关User模型,为此,你需要使用join我不知道你的结构,所以我只是给你一个例子。 Suppose you want to sort by profiles => id then you can write something: 假设您要按profiles => id排序,则可以编写一些内容:

Edit: As per the requirement given in comments I would recommend to have something like this: First of all lets have authenticated user by 编辑:根据注释中给出的要求,我建议使用类似以下内容的内容:首先,通过以下方式对用户进行身份验证:

$AuthUser = Auth::user();

Then lets call the relation 然后叫关系

$users = User::->leftJoinRelations('profile')
    ->orderByRaw("FIELD(country , ". $AuthUser->profile->country ."), ASC")
    ->paginate();

So in this way you can have user sorted by authenticated user country. 因此,您可以通过已验证用户所在的国家/地区对用户进行排序。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM