HasmanyThrough查询没有Laravel_Through_key

发布于 2025-01-21 16:52:01 字数 1427 浏览 0 评论 0原文

有了Laravel Hasmanythough的关系,我如何防止添加额外的密钥?有没有pivot或simiar的?

我正在尝试执行联盟加入,并且需要将列保持相同。

SQLSTATE [21000]:违反基数:1222使用的选择语句有不同数量的列,

SELECT
    count(*) AS aggregate
FROM ((
        SELECT
            `order_payments`.`id`,
            `order_payments`.*,
            `orders`.`user_id` AS `laravel_through_key`
        FROM
            `order_payments`
            INNER JOIN `orders` ON `orders`.`id` = `order_payments`.`order_id`
        WHERE
            `orders`.`user_id` = 1)
    UNION (
        SELECT
            `orders`.`id`
        FROM
            `orders`
        WHERE
            `orders`.`user_id` = 1
            AND `orders`.`user_id` IS NOT NULL)) AS `temp_table`

我无法使用Make -Make -Hidden,如另一个问题所建议的,

$payments = auth()->user()->orderPayments()->select('order_payments.id');

$orders = auth()->user()->orders()->select('orders.id');

$payments->union($orders)->paginate(50);

我想我可以在不手动的情况下做到这一点,但只是想知道还有另一种方法吗?

class User extends Authenticatable implements UserContract {
    /**
     * Orders for this User
     */
    public function orders()
    {
        return $this->hasMany(Order::class);
    }

    /**
     * Order Payments for this User
     */
    public function orderPayments()
    {
        return $this->hasManyThrough(OrderPayment::class, Order::class);
    }
}

With a Laravel hasManyThrough relationship how can I prevent extra keys added to the select? Is there a withoutPivot or simiar?

I'm trying to perform a union join and need to the columns to be the same.

SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns

SELECT
    count(*) AS aggregate
FROM ((
        SELECT
            `order_payments`.`id`,
            `order_payments`.*,
            `orders`.`user_id` AS `laravel_through_key`
        FROM
            `order_payments`
            INNER JOIN `orders` ON `orders`.`id` = `order_payments`.`order_id`
        WHERE
            `orders`.`user_id` = 1)
    UNION (
        SELECT
            `orders`.`id`
        FROM
            `orders`
        WHERE
            `orders`.`user_id` = 1
            AND `orders`.`user_id` IS NOT NULL)) AS `temp_table`

I'm unable to use the makeHidden as suggested in another question

$payments = auth()->user()->orderPayments()->select('order_payments.id');

$orders = auth()->user()->orders()->select('orders.id');

$payments->union($orders)->paginate(50);

I guess I could do it without Eloquent manually but just wondering if there was another way?

class User extends Authenticatable implements UserContract {
    /**
     * Orders for this User
     */
    public function orders()
    {
        return $this->hasMany(Order::class);
    }

    /**
     * Order Payments for this User
     */
    public function orderPayments()
    {
        return $this->hasManyThrough(OrderPayment::class, Order::class);
    }
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文