如何使用雄辩的命令基于数据库关系订购json响应 [英] how to order a json response based on database relationship using eloquent

查看:53
本文介绍了如何使用雄辩的命令基于数据库关系订购json响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Laravel还是很陌生,我一直在努力弄清到目前为止如何与Eloquent一起正常工作,但是我陷入了我想做的事情:

i'm quite new to Laravel and i'm triying to figure out how to properly work with Eloquent so far so good, but i'm stuck in something i want to do:

如果您将迁移数计算在内,那么我在数据库井4中有3个表:food,food_group和一部分,food_group和part的结构相同,id和ID与主键和名称列相同

I have 3 tables in a database well 4 if you count the migrations one: food, food_group and portions wich food_group and portion have the same structure wich is and id as primary key and a name columns

食物有

 |  id(primary)  |  name  | food_group_id (foreign key) | portion_id (foreign key |

很好,因为我在自己的路线上有一个很好的格式化杰森

all good cause i have a nice formated Jason with this in my route

Route::get('/read', function() {

$categories = App\FoodGroup::with('Foods')->get();

 return Response::json(array('data' => $categories));
});

data: [
    {
       id: 1,
       name: "Frutas",
       foods: [
               {
                   id: 18,
                   name: "Acelga",
                   cant_portion: 2
               },
               {
                   id: 19,
                   name: "Espinaca",
                   cant_portion: 2
               },
              ]
    }
 ]

如果我用App \ Portion更改App \ FoodGroup,它会给我相同的数组,但现在与前键部分一起订购

and if i changue the App\FoodGroup with App\Portion it gives me the same array but now ordered as the Foreing Key Portion

我需要首先通过FoodGRoup进行订购,并在FFo_group的每个项目中进行订购,现在由第二个Foreing键订购,这是Portion,所以我可以拥有类似的东西

what i need is to first order with FoodGRoup and inside each item of the FFo_group one, now ordered by the second Foreing key wich is Portion so i can have something like this

data: [
    {
       id: 1,
       name: "Frutas",
       portions:[
           id: 18,
           name: "Gr",  
           foods: [
               {
                   id: 18,
                   name: "Acelga",
                   cant_portion: 2
               },
               {
                   id: 19,
                   name: "Espinaca",
                   cant_portion: 2
               },
            ]
       ]           
    }
 ]

推荐答案

您期望的JSON显示foods作为portions的子级.为此,您需要设置此关系.

Your expected JSON shows foods as a child of portions. In order to do this, you need to setup this relationship.

在您的Portion模型上,您需要设置以下关系:

On your Portion model, you need to setup the following relationship:

public function foods() {
    return $this->hasMany(Food::class);
}

通过这种关系设置,您现在可以像这样获取数据:

With this relationship setup, you can now get your data like this:

$categories = App\FoodGroup::with('portions.foods')->get();

这将装入您的食物组,然后将这些部分装入食物组,然后将食物装入这些部分.

This will load your food groups, then it will load the portions into the food groups, and then it will load the foods into the portions.

我可能对您的问题有误读.我假设您在\App\FoodGroup上定义了portions关系.如果没有,则可以这样添加:

I may have slightly misread your question. I assumed you had a portions relationship defined on \App\FoodGroup. If not, you can add this like so:

FoodGroup:

FoodGroup:

public function portions() {
    // the second parameter is the name of the pivot table.
    // in this case, your foods table connects your portions and food groups.
    return $this->belongsToMany(Portion::class, 'foods')->distinct();
}

编辑2

此解决方案有点笨拙,因为它将foods表视为数据透视表,尽管它不是专门为此设计的.因此,foods表中有多个条目包含相同的密钥对值,这就是为什么您得到重复的相关模型的原因.

Edit 2

This solution is a little hacky because it is treating the foods table as a pivot table, though it wasn't specifically designed for that. Because of this, there are multiple entries in the foods table that contain the same key pair values, and this is why you're getting duplicate related models.

如果将distinct()放在关系上,则应解决此问题,因为它将消除从内部联接创建的重复项.上面的代码已被修改.

If you throw a distinct() onto the relationship, this should take care of the issue, as it will eliminate the duplicates created from the inner join. The code above has been modified.

这篇关于如何使用雄辩的命令基于数据库关系订购json响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆