Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用户拥有许多用户TroughtPivotLaravel7.x_Laravel_Pivot Table_Has Many Through - Fatal编程技术网

用户拥有许多用户TroughtPivotLaravel7.x

用户拥有许多用户TroughtPivotLaravel7.x,laravel,pivot-table,has-many-through,Laravel,Pivot Table,Has Many Through,好的,我这里有个问题,本来应该是简单的,但没有想到这一点 我有一个用户表 $table->increments('id'); $table->string('name'); $table->string('email')->unique()->nullable(); $table->string('telefono')->nullable();

好的,我这里有个问题,本来应该是简单的,但没有想到这一点

我有一个用户表

            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique()->nullable();
            $table->string('telefono')->nullable();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
每个用户都有一个静态角色

Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('slug');
            $table->timestamps();
        });

比如说 用户Jhon具有家庭领导者的角色 和用户:

  • 艾米丽 标记 Randy 橡胶
  • 在数据透视表上注册子角色

    Schema::create('relation', function (Blueprint $table) {
                $table->unsignedInteger('user_id');
                $table->unsignedInteger('relation_id');
    
             //FOREIGN KEY CONSTRAINTS
               $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
               $table->foreign('relation_id')->references('id')->on('users')->onDelete('cascade');
    
            });
    
    因此,User_id是关系的父对象,每个父对象都有在表中注册的拍卖属性

    Schema::create('auction', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name');
                $table->text('description');
                $table->unsignedInteger('user_id');
                $table->timestamp('endate');
    
             //FOREIGN KEY CONSTRAINTS
               $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    
                $table->timestamps();
            });
    
    这个表上的用户id是父用户id

    好吧,我的问题是,当一个孩子登录时,你可以得到一个列表上的所有拍卖。所以我做了一些关系

    User Model
    
    所以我想打电话给你

    $auctions = $user->auctions()
            ->orderBy('created_at', 'desc')
            ->first();
    
    $auctions = $user->auctions()
            ->orderBy('created_at', 'desc')
            ->first();