简体   繁体   English

如何在 Laravel 5.8 中基于多对多关系查找数据

[英]How to find data based on Many To Many relationship in Laravel 5.8

I have a Many To Many relationship between User Model & Wallet Model:我在用户模型和钱包模型之间有一个多对多的关系:

Wallet.php : Wallet.php :

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

And User.php :User.php

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

And I have these three tables related to Wallets:我有这三个与钱包相关的表格:

Table wallets :桌上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();
        });
    }

Table user_wallet :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();
        });
    }

And table user_wallet_transactions :和表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();
        });
    }

Now I need to show wallets of a single user.现在我需要显示单个用户的钱包。 So at the users.index Blade, I added this:因此,在users.index Blade 中,我添加了以下内容:

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

And send the user data to Controller like this:并将用户数据发送到控制器,如下所示:

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

But I don't know how to retrieve user_wallet information at this Method.但我不知道如何在此方法中检索user_wallet信息。

So how to get data from user_wallet in this situation.那么在这种情况下如何从user_wallet获取数据。

I would really appreciate any idea or suggestion from you guys about this...我真的很感激你们对此的任何想法或建议......

Thanks in advance.提前致谢。

One way is accept param as $id一种方法是接受param$id

public function index($id)

then然后

User::with('wallets')->has('wallets')->find($id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从一对多关系访问 laravel 5.8 中的数据 - Couldn't access data in laravel 5.8 from 1 to many relationship Laravel 5.8不使用DB :: raw()的雄辩查询—根据多对多关系的最新值获取行 - Laravel 5.8 Eloquent query without using DB::raw() — get rows based on latest value of many to many relationship Laravel 5.2 基于多对多关系寻找模型 - Laravel 5.2 Find a Model Based on a Many-to-Many relationship Laravel 5.8:如何在多对多关系中显示数据透视表的列信息 - Laravel 5.8: How to show a column information of a pivot table in Many To Many relationship 查询基于AND或OR的Laravel多对多关系 - Query Laravel many-to-many relationship based on AND not OR 无法在 Laravel 5.8 中以一对多关系更新表 - Unable to update tables in one-to-many relationship in laravel 5.8 Laravel 5.8:如何上载,下载和删除链接到员工的文档(一对多关系) - Laravel 5.8: How to upload, download, and delete documents thats link to an employee (1-to-many relationship) 如何访问数据透视表的数据(多对多关系laravel) - How to access data of a pivot table ( many to many relationship laravel ) 如何在许多关系中访问所有表数据 - How to access all table data in many many relationship laravel laravel中多对多关系如何正确检索数据? - How correctly retrieve the data in laravel in many to many relationship?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM