简体   繁体   English

将 id 从第一个模型传递到 Eloquent

[英]Pass id from first model into a Eloquent

I have a problem wanting to pass the id of Products in the subqueries.我想在子查询中传递产品的 id 时遇到问题。

The first code is what I have so far.第一个代码是我到目前为止所拥有的。 The second is the way I want to do with Eloquent, but I can't.第二种是我想用 Eloquent 做的事情,但我不能。

      $result = [];

    Product::with(['locals.presentations'])->each(function ($product) use (&$result) {

        $body['id'] = $product->id;
        $body['nombre'] = $product->nombre;
        $sedes = [];
        $product->locals->each(function ($local) use (&$sedes, $product) {
            $presentations = [];
            $local->presentations->each(function ($presentation) use (&$presentations, $local, $product) {

                if ($presentation->local_id == $local->id && $presentation->product_id == $product->id) {
                    $presentations[] = [
                        'local_id' => $presentation->local_id,
                        'product_id' => $presentation->product_id,
                        'presentacion' => $presentation->presentation,
                        'precio_default' => $presentation->price
                    ];
                }
            });

          ...
    });

    return $result;

I want transform the previous code into this with Eloquent, but I can't pass the product_id into the subqueries:我想用 Eloquent 将之前的代码转换成这个,但我无法将 product_id 传递到子查询中:

    $products = Product::with(['locals' => function ($locals) {
        //How to get the id from Product to pass in the $presentations query ??????
        $locals->select('locals.id', 'descripcion')
            ->with(['presentations' => function ($presentations) {
                $presentations
                // ->where('presentations.product_id', $product_id?????)
                ->select(
                    'presentations.local_id',
                    'presentations.product_id',
                    'presentations.id',
                    'presentation',
                    'price'
                );
            }]);
    }])->select('products.id', 'nombre')->get();

    return $products;

You can simply use the has() method if you have set the relations correctly on the Product and Local models.如果您在ProductLocal模型上正确设置了关系,则可以简单地使用has()方法。 This will return ONLY the products which has locals AND presentations.这将仅返回具有当地人和演示文稿的产品。

If you want every product but only the locals and presentations with the product_id equals to the products.id, then you don't have to do anything.如果您想要所有产品,但只想要产品 ID 等于 products.id 的 locals 和演示文稿,那么您无需执行任何操作。 The relationship you set in your models already checks if the id matches.您在模型中设置的关系已经检查了 id 是否匹配。

$products = Product::has('locals.presentations')
    ->with(['locals' => function ($locals) {
        $locals
            ->select('locals.id', 'descripcion')
            ->with(['presentations' => function ($presentations) {
                $presentations->select(
                    'presentations.local_id',
                    'presentations.product_id',
                    'presentations.id',
                    'presentation',
                    'price'
                );
           }]);
    }])->select('products.id', 'nombre')->get();

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

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