【问题标题】:Pass id from first model into a Eloquent将第一个模型的 id 传递给 Eloquent
【发布时间】:2021-12-23 14:27:48
【问题描述】:

我想在子查询中传递 Products 的 id 时遇到问题。

第一个代码是我目前所拥有的。第二种是我想用 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;

我想用 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;

产品

    public function locals()
{

    return $this->belongsToMany(Local::class)->using(LocalProduct::class)
        ->withPivot(['id', 'is_active'])
        ->withTimestamps();

}

本地

    public function presentations()
{
    return $this->hasManyThrough(
        Presentation::class,
        LocalProduct::class,
        'local_id',
        'local_product_id'
    );
}

【问题讨论】:

    标签: php mysql laravel eloquent


    【解决方案1】:

    如果您在ProductLocal 模型上正确设置了关系,则可以简单地使用has() 方法。这将仅返回具有本地人和演示文稿的产品。

    如果您想要所有产品,但只需要 product_id 等于 products.id 的本地人和演示文稿,那么您无需执行任何操作。您在模型中设置的关系已经检查了 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();
    

    【讨论】:

    • 我的代码结果相同...:/。我更新了我的模型
    猜你喜欢
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多