除了某些特定行以外

发布于 2025-02-08 21:39:47 字数 1032 浏览 2 评论 0 原文

我正在建立一个API,以升级顺序从数据库返回商店,除非它应该从所有名为Swiva的商店开始,例如Swiva Electronics,Swiva Gas,Swiva Mall等。 我已经尝试了以下代码

       $shops=Shop::join('users','users.id','=','shops.owner_id')
        ->select('shops.id','shops.name','users.email','users.tel','users.estate','shops.logo')
        ->orderBy('shops.name')
        ->get()
        ->partition(function ($item) {
            return $item->name != 'Swiva Electronics';
        })->flatten();

,但这并不是我想要的,因为

  1. 它不是从预期的行开始的,而是以此为止。
  2. 它仅与一行一起工作,我不知道如何指定所有其他行

请注意,此查询的输出作为JSON返回,如下所示,

        if(!$shops->isEmpty()){
            return response()->json([
            'success'=>true,
            'shops'=> $shops
        ]);
    }else if($shops->isEmpty()){
            return response()->json([
            'success'=>false,
            'message'=> 'There exists no subscribed shops from '
            ]);
        } 

我该如何通过格式化JSON来实现预期的输出(我不知道该怎么做)或使用雄辩的功能。

I'm building an API to return shops from a database in ascending order except that it should begin with all shops named swiva eg Swiva Electronics, Swiva Gas, Swiva Mall etc. then proceed to all the other shops in ascending order.
I have tried the following code

       $shops=Shop::join('users','users.id','=','shops.owner_id')
        ->select('shops.id','shops.name','users.email','users.tel','users.estate','shops.logo')
        ->orderBy('shops.name')
        ->get()
        ->partition(function ($item) {
            return $item->name != 'Swiva Electronics';
        })->flatten();

but that is not doing what I want as

  1. It is not beginning with the intended row rather it is ending with it.
  2. It is only working with one row only, I do not know how to specify all the other rows

Note that the output of this query is being returned as a json as follows

        if(!$shops->isEmpty()){
            return response()->json([
            'success'=>true,
            'shops'=> $shops
        ]);
    }else if($shops->isEmpty()){
            return response()->json([
            'success'=>false,
            'message'=> 'There exists no subscribed shops from '
            ]);
        } 

How can I achieve the intended output either through formatting the JSON( Which I do not know how to do) or by using eloquent functions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小梨窩很甜 2025-02-15 21:39:47

您可以使用MySQL的现场函数来实现此目的,请参见:

$shops = Shop::join('users','users.id','=','shops.owner_id')
        ->select('shops.id', 'shops.name', 'users.email', 'users.tel', 'users.estate', 'shops.logo')
        ->orderByRaw("FIELD(shops.`name`, 'Swiva Electronics', 'Swiva Gas', 'Swiva Mall') DESC, shops.`name`")
        ->get()

由于我们无法在字段选项中添加所有名称,默认情况下它将列出所有其他名称首先使用desc desc and deSc andver invert invert invert invert invert invert unvert unvert it inver it unvert unvert unvert unvert unvert unvert it in actuct in Resulte首先移动这些名称通过按商店名称添加简单的ASC订单来名称。

You can use the Field Function of MySQL to achieve this see: https://www.w3schools.com/sql/func_mysql_field.asp

$shops = Shop::join('users','users.id','=','shops.owner_id')
        ->select('shops.id', 'shops.name', 'users.email', 'users.tel', 'users.estate', 'shops.logo')
        ->orderByRaw("FIELD(shops.`name`, 'Swiva Electronics', 'Swiva Gas', 'Swiva Mall') DESC, shops.`name`")
        ->get()

As we can't add all names in FIELD options by default it will list all other names first Use DESC order to invert it will move those names first in results and then sort the all other shop names by adding simple ASC order by shop names.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文