Sql 根据与其他表的关系,仅显示特定于用户的用户

Sql 根据与其他表的关系,仅显示特定于用户的用户,sql,laravel,Sql,Laravel,我正在尝试列出注册主题的用户。所以我有这些表格: 用户: Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('email')->unique(); $table->timestam

我正在尝试列出注册主题的用户。所以我有这些表格:

用户:

Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->boolean('admin')->default(false);
            $table->unsignedInteger('year');
            $table->unsignedBigInteger('department_id');
            $table->rememberToken();
            $table->timestamps();

            //$table->foreign('department_id')->references('id')->on('departments');
        });
主题:

Schema::create('subjects', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->string('name');
            $table->string('text');
            $table->unsignedInteger('year');
        });
这张表应该在这两者之间建立一种关系:

Schema::create('subject_user', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->unsignedBigInteger('subject_id');
            $table->unsignedBigInteger('user_id');

            $table->foreign('subject_id')->references('id')->on('subjects')->onDelete('cascade');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

        });
型号:

主题:

public function users(){
        return $this->hasMany('App\User');
    }
用户:


现在,如果我单击某个具有某个id的主题,它会将我重定向到一个页面,其中列出了该主题中的用户。但我可以;我似乎没能使它起作用。我尝试了类似于
@foreach($subject->users()->get()as$user){{{$user->name}}@endforeach
,但不起作用。

subject模型中,编写如下关系:

public function users()
{
    return $this->belongsToMany(User::class);
}
$users = User::has('subjects')->get();
$users = User::whereHas('subjects', function($q) use ($subject_id){
    $q->where('subjects.id', $subject_id);
})->get();

然后,您可以在控制器中获得如下主题:

$subjects = Subject::latest()->with('users')->get();
如需了解结果,请使用
返回$subjects

对于视图中的单个主题,请使用:

@foreach($subject->users() as $user) 
    {{$user->name}}
@endforeach
主题模式:

public function users(){
        return $this->belongsToMany('App\User');
    }
用户模型:

public function subjects() {
        return $this->belongsToMany('App\Subject');
    }
在控制器中,您可以使用如下方法:

public function users()
{
    return $this->belongsToMany(User::class);
}
$users = User::has('subjects')->get();
$users = User::whereHas('subjects', function($q) use ($subject_id){
    $q->where('subjects.id', $subject_id);
})->get();
如果您想在WhereHas方法中作为闭包执行一些自定义筛选器传递,例如:

$users = User::whereHas('subjects', function($q){
    $q->where('created_at', '>=', '2019-09-12');
})->get();
如果您希望获得订阅特定主题的用户,您应该将筛选器传递到whereHas,如我上面所述,如下所示:

public function users()
{
    return $this->belongsToMany(User::class);
}
$users = User::has('subjects')->get();
$users = User::whereHas('subjects', function($q) use ($subject_id){
    $q->where('subjects.id', $subject_id);
})->get();

所以我想我做到了。我将模型更改为:

public function users(){
        return $this->belongsToMany('App\User');
    }
然后在视图中:

@foreach ($subject->users()->where('subject_id', $subject->id)->get() as $user)
<h1>{{$user->name}}</h1>
@endforeach
@foreach($subject->users()->where('subject\u id',$subject->id)->get()作为$user)
{{$user->name}
@endforeach

请在你的模型中写下你的关系,正如@mohammadHosseini所说的,你在你的模型中创建了关系吗?你能给我们展示一下
dd($subject->users())的结果吗▼   #表:“subject_user”#foreignPivotKey:“subject_id”#relatedPivotKey:“user_id”#parentKey:“id”#relatedKey:“id”#relationName:“users”#pivotColumns:[]#pivotWhere:[]#pivotWhere:[]#pivotValues:[+带时间戳:false#创建数据透视:null#更新数据透视:null#使用:null访问器#数据透视查询:Builder{349▶}   #家长:受试者{#342▶}   #相关:用户{#338▶}   -currentlyAttached:null}
但是我如何才能看到该主题的所有用户?@endyscar,因为您没有传递主题以正确查看