0

我在用户模型和钱包模型之间存在多对多关系:

Wallet.php

public function users()
    {
        return $this->belongsToMany(User::class);
    }

并且User.php

public function wallets()
    {
        return $this->belongsToMany(Wallet::class);
    }

我有这三个与钱包相关的表:

wallets

public function up()
    {
        Schema::create('wallets', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->string('name')->unique();
            $table->tinyInteger('is_active');
            $table->tinyInteger('is_cachable');
            $table->timestamps();
        });
    }

user_wallet

public function up()
    {
        Schema::create('user_wallet', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('usr_id')->on('users');
            $table->unsignedBigInteger('wallet_id');
            $table->foreign('wallet_id')->references('id')->on('wallets');
            $table->integer('balance');
            $table->timestamps();
        });
    }

和表user_wallet_transactions

public function up()
    {
        Schema::create('user_wallet_transactions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('usr_id')->on('users');
            $table->unsignedBigInteger('wallet_id');
            $table->foreign('wallet_id')->references('id')->on('wallets');
            $table->string('amount');
            $table->string('description');
            $table->timestamps();
        });
    }

现在我需要显示单个用户的钱包。所以在users.indexBlade 中,我添加了以下内容:

<a href="{{ route('user.wallet', $user->usr_id) }}" class="fa fa-wallet text-dark"></a>

并将用户数据发送到控制器,如下所示:

public function index(User $user)
    {
        // retrieve user_wallet information
        return view('admin.wallets.user.index', compact(['user']));
    }

但我不知道如何user_wallet在此方法中检索信息。

那么如何user_wallet在这种情况下获取数据。

我真的很感激你们关于这个的任何想法或建议......

提前致谢。

4

1 回答 1

0

一种方法是接受param$id

public function index($id)

然后

User::with('wallets')->has('wallets')->find($id);
于 2021-07-14T05:39:58.677 回答