Php Laravel多表数据过滤器
我使用的是Laravel 5,我有3个数据库表Php Laravel多表数据过滤器,php,database,laravel,Php,Database,Laravel,我使用的是Laravel 5,我有3个数据库表项目,应用程序,用户,它们的表结构如下: 项目 Schema::create('projects', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->text('description'); $table->char('status',1
项目
,应用程序
,用户
,它们的表结构如下:
项目
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('description');
$table->char('status',1);
$table->timestamps();
});
Schema::create('applications', function (Blueprint $table) {
$table->increments('id');
$table->integer('project_id')->unsigned()->index();
$table->integer('user_id')->unsigned()->index();
$table->char('status');
$table->timestamps();
});
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('role');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
应用程序
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('description');
$table->char('status',1);
$table->timestamps();
});
Schema::create('applications', function (Blueprint $table) {
$table->increments('id');
$table->integer('project_id')->unsigned()->index();
$table->integer('user_id')->unsigned()->index();
$table->char('status');
$table->timestamps();
});
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('role');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
用户
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('description');
$table->char('status',1);
$table->timestamps();
});
Schema::create('applications', function (Blueprint $table) {
$table->increments('id');
$table->integer('project_id')->unsigned()->index();
$table->integer('user_id')->unsigned()->index();
$table->char('status');
$table->timestamps();
});
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('role');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
现在,我想执行一个查询,返回特定用户尚未向该项目提交任何应用程序的所有项目。到目前为止,这是我能得到的最接近的结果,但是对于没有应用程序的每个项目,它将在projects.id
中返回NULL
DB::table('projects')
->leftjoin('applications', 'applications.project_id', '=', 'projects.id')
->where('applications.user_id', '!=', $user->id)
->orWhereNull('applications.id')
->get();
查询结果
{
"total":1,
"per_page":5,
"current_page":1,
"last_page":1,
"next_page_url":null,
"prev_page_url":null,
"from":1,"to":1,
"data":[{
"id":null,
"name":"Project2",
"description":"Project2 detail",
"status":null,
"created_at":null,
"updated_at":null,
"project_id":null,
"user_id":null
}]
}
$projects_not_applied = DB::table('projects')
->whereNotIn('id',$user_applications)
->get();
有人能解决这个问题吗
DB::table('projects')
->leftjoin('applications', function($join){
$join->on('applications.project_id', '=', 'projects.id')
->where('applications.user_id', '=', $user->id);
})
->whereNull('applications.id')
->get();
我想这就是你想要的我只是想通过使用两个不同的查询并将它们组合在一起来解决这个问题 1)选择所有用户应用程序的项目id
$user_applications = Application::where('user_id', '=', $user->id)
->select('project_id as id')
->get();
2)选择项目不在上一列表中的项目
{
"total":1,
"per_page":5,
"current_page":1,
"last_page":1,
"next_page_url":null,
"prev_page_url":null,
"from":1,"to":1,
"data":[{
"id":null,
"name":"Project2",
"description":"Project2 detail",
"status":null,
"created_at":null,
"updated_at":null,
"project_id":null,
"user_id":null
}]
}
$projects_not_applied = DB::table('projects')
->whereNotIn('id',$user_applications)
->get();
查询结果(用户未申请项目2):
这个查询仍然给了我与
project.id
=NULL
相同的结果:(您是如何试图检索该值的?)对不起,这是什么意思?我不太明白您的问题