如何在拉拉维尔(Laravel)中对许多人建立反向关系

发布于 2025-01-28 09:46:10 字数 1930 浏览 4 评论 0原文

我有两个模型是用户和交易,我已经建立了一段关系,我的Hasmany关系正常工作,但是它的反向关系不起作用,这是我的交易迁移。

        Schema::create('transactions', function (Blueprint $table) {
            $table->id();
            $table->string('transaction_id');
            $table->foreignId('transaction_from')
            ->constrained('users')
            ->onUpdate('cascade')
            ->onDelete('cascade');

            $table->foreignId('transaction_to')
            ->constrained('users')
            ->onUpdate('cascade')
            ->onDelete('cascade');

            $table->integer('transaction_coins');
            $table->string('transaction_note');
            $table->boolean('transaction_status');
            $table->timestamps();
        });

这是我的用户模型,我创建了两个关系。

    public function transactions_from()
    {
        return $this->hasMany(Transaction::class, 'transaction_from');
    }

    public function transactions_to()
    {
        return $this->hasMany(Transaction::class, 'transaction_to');
    }

这很好地工作了,我从交易和交易中获得了交易,但是当我创建反向关系功能时,我没有得到所需的结果。

    public function user_from()
    {
        return $this->belongsTo(User::class, 'transaction_from', 'id');
    }

    public function user_to()
    {
        return $this->belongsTo(User::class, 'transaction_to', 'id');
    }

在我的刀片文件中,我想显示从交易中进行交易并用交易显示的用户名称的名称。

<td>
                                <div class="d-flex align-items-center">
                                    <img src="{{ asset('assets\images\table\pic-1.jpg') }}" class="rounded-circle me-2 width36 height36" alt="">
                                    <span>{{ $transaction->transaction_from->user_from->name }}</span>
                                </div>
                            </td>

请给我建议我错了。

I have two models that are User and Transactions and I have made a relationship OneToMany, my hasMany Relationship is working fine, but its reverse relationship is not working here is my Transaction Migration.

        Schema::create('transactions', function (Blueprint $table) {
            $table->id();
            $table->string('transaction_id');
            $table->foreignId('transaction_from')
            ->constrained('users')
            ->onUpdate('cascade')
            ->onDelete('cascade');

            $table->foreignId('transaction_to')
            ->constrained('users')
            ->onUpdate('cascade')
            ->onDelete('cascade');

            $table->integer('transaction_coins');
            $table->string('transaction_note');
            $table->boolean('transaction_status');
            $table->timestamps();
        });

and Here is my User Model, I created two Relationships.

    public function transactions_from()
    {
        return $this->hasMany(Transaction::class, 'transaction_from');
    }

    public function transactions_to()
    {
        return $this->hasMany(Transaction::class, 'transaction_to');
    }

This is working fine I got Transactions From and Transactions To but when I created Reverse Relationship Functions I am not getting required results.

    public function user_from()
    {
        return $this->belongsTo(User::class, 'transaction_from', 'id');
    }

    public function user_to()
    {
        return $this->belongsTo(User::class, 'transaction_to', 'id');
    }

and in my blade file I want to show the Name of the User who Made Transaction From and To show the User Name with Transaction To.

<td>
                                <div class="d-flex align-items-center">
                                    <img src="{{ asset('assets\images\table\pic-1.jpg') }}" class="rounded-circle me-2 width36 height36" alt="">
                                    <span>{{ $transaction->transaction_from->user_from->name }}</span>
                                </div>
                            </td>

Please suggest me where I am wrong.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梅窗月明清似水 2025-02-04 09:46:10

您已经在交易模型上与用户模型上的user_to和user_th有关系。

<td>
    <div class="d-flex align-items-center">
         <img src="{{ asset('assets\images\table\pic-1.jpg') }}" class="rounded-circle me-2 width36 height36" alt="">
           <span>{{ $transaction->transaction_from->user_from->name }}</span>
     </div>
</td>

而且,如果您的刀片$交易是您的交易模型对象,则应该这样做:

<td>
  <div class="d-flex align-items-center">
     <img src="{{ asset('assets\images\table\pic-1.jpg') }}" class="rounded-circle me-2 width36 height36" alt="">
       <span>{{ $transaction->user_from->name }}</span>
  </div>
</td>

you have made relation on your Transaction model with user_to and user_from on User model.

<td>
    <div class="d-flex align-items-center">
         <img src="{{ asset('assets\images\table\pic-1.jpg') }}" class="rounded-circle me-2 width36 height36" alt="">
           <span>{{ $transaction->transaction_from->user_from->name }}</span>
     </div>
</td>

and if here on your blade $transaction is your object of Transaction model you should do like this:

<td>
  <div class="d-flex align-items-center">
     <img src="{{ asset('assets\images\table\pic-1.jpg') }}" class="rounded-circle me-2 width36 height36" alt="">
       <span>{{ $transaction->user_from->name }}</span>
  </div>
</td>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文