【问题标题】:Column not found: 1054 Unknown column 'coin_user.symbol' in 'field list'未找到列:1054“字段列表”中的未知列“coin_user.symbol”
【发布时间】:2021-10-24 02:43:17
【问题描述】:

我们有三个模型(用户-货币-余额)这些表应该是什么关系,比如我要接收用户的比特币货币数量 为此,我编写了如下代码(根据我从上一个问题中得到的帮助)

这是用户表:

Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->enum('type', [User::TYPE_ADMIN, User::TYPE_USER])->default(User::TYPE_USER);
        $table->string('name');
        $table->string('email')->unique()->nullable();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password')->nullable();
        $table->timestamp('password_changed_at')->nullable();
        $table->rememberToken();
        $table->timestamps();
        $table->softDeletes();
    });

硬币桌:

 Schema::create('coins', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('symbol')->unique();
        $table->decimal('buy_percent',6,4)->nullable();
        $table->enum('buy_status' , ['active' , 'inactive'])->default('inactive');
        $table->text('buy_description')->nullable();
        $table->decimal('sell_percent',6,4)->nullable();
        $table->enum('sell_status' , ['active' , 'inactive'])->default('inactive');
        $table->text('sell_description')->nullable();
        $table->timestamps();
    });

和余额表。我在此迁移中包含数据透视表:

        Schema::create('balances', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->unsignedBigInteger('coin_id');
            $table->string('symbol');
            $table->decimal('balance');
            $table->timestamps();
        });
        Schema::create('coin_user', function(Blueprint $table)
        {
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->unsignedBigInteger('coin_id');
            $table->foreign('coin_id')->references('id')->on('coins')->onDelete('cascade');
        });

关系是这样写的:

在用户模型中:

    public function coins() {
        return $this->belongsToMany(Coin::class)->withPivot(['symbol', 'balance']);
    }

在硬币模型中:

    public function users() {
        return $this->belongsToMany(User::class)->withPivot(['symbol', 'balances']);
    }

和平衡模型:

    public function coins()
    {
        return $this->belongsToMany(Coin::class , 'balances');
    }

    public function user()
    {
        $this->belongsToMany(User::class , 'balances');
    }

当我运行这段代码时:

    $coin = Coin::with('users')->get();
    foreach ($coin as $coin) {
        // $coin here is `BTC`, `ETH`, etc.
        foreach ($coin->users as $user) {
            // $user here is Bob, Mike, etc.
            $user->pivot->balance; // 0.16, etc.
            // Do whatever with `$coin` and `$user`
        }
    }

为我返回此错误:

SQLSTATE[42S22]:未找到列:1054 '字段列表'中的未知列'coin_user.symbol'(SQL:选择users.*、coin_user.coin_idpivot_coin_idcoin_user .user_id as pivot_user_id, coin_user.symbol as pivot_symbol, coin_user.balances as pivot_balances from users inner join coin_user on @986@986@ coin_user.user_id 其中coin_user.coin_id in (1, 2, 3, 4) 和users.deleted_at 为空)

有什么问题?如何访问用户的余额?

【问题讨论】:

  • “根据我从上一个问题收到的帮助” - 您从上一个问题收到的帮助说您的表格方法对 belongsToMany().. 无效. 另外,在你请求的聊天中没有看到你,想如果你有更多的跟进,你会再次 ping 吗????
  • 非常抱歉..我以为你没有看到聊天请求,我觉得我的问题太长了,因为我创建了一个新问题。我为疏忽道歉。我不是那个意思。我就是不能很好的说和听懂英语????我不明白你的意思is not valid with belongsToMany ()@TimLewis
  • 别担心,我认为是这样。仅出于整理目的,两次打开相同的问题并不是很好(老实说,可能只是删除旧问题)。 “无效”是指belongsToMany() 不适用于作为 3 个模型(硬币、余额和用户)之间的枢轴的数据透视表。您上面使用的代码可以与 3 个表/2 个模型一起使用,coinsusersbalances,以及 CoinUser(经过一些调整),但是因为您添加了 balances 作为模型和coin_user 作为另一个支点,它是无效的。
  • 当然。我会删除它。那有什么解决办法,我该怎么办? @蒂姆刘易斯
  • 要么重新考虑你的表结构(让balances 成为userscoins 之间的枢轴,并用一个列balance 来跟踪平衡),或者调整你的模型和关系不使用belongsToMany(),而是使用hasMany()belongsTo()等。

标签: php laravel relationship


【解决方案1】:

在 Laravel 中,如果两个模型要具有belongsToMany 关系,那么这两个模型必须存在一个数据透视表。缺少数据透视表(假设您没有在问题中提供任何数据透视表架构)是这里的第一个问题。

【讨论】:

    猜你喜欢
    • 2013-05-26
    • 2021-12-03
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 2017-08-09
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多