我使用Laravel 5和我有3個數據庫表projects
,applications
,users
及其表結構是如下:Laravel多個表數據濾波器
項目
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
}]
}
有沒有人有一個解決的辦法?
這個查詢仍然給我與'project.id' ='NULL'相同的結果:( – Woody
你怎麼試圖檢索那個值 – Chris
對不起,你是什麼意思?我不太明白你的問題 – Woody