【问题标题】:Selecting from multiple tables in laravel从 laravel 中的多个表中选择
【发布时间】:2021-12-23 00:30:53
【问题描述】:

请帮忙。我有一张像这样的考试预约表

Schema::create('test_bookings', function (Blueprint $table) {
        $table->unsignedInteger('RequestID');
        $table->string('bookingDate');
        $table->string('timeSlot');
        $table->unsignedInteger('nurse_id');
        $table->timestamps();
    });

还有一个看起来像这样的测试表

Schema::create('tests', function (Blueprint $table) {
        $table->unsignedInteger('RequestID');
        $table->unsignedInteger('patientID');
        $table->string('barcode');
        $table->string('temperature');
        $table->string('pressure');
        $table->string('oxygen');
        $table->unsignedInteger('nurseID');
        $table->timestamps();
    });

只有当 test_bookings RequestID 在测试表中时,我才想显示护士的 RequestID、bookingDate、timeSlot、姓名和姓氏。这是我的护士桌

Schema::create('nurses', function (Blueprint $table) {
        $table->unsignedInteger('nurseID');
        $table->string('name');
        $table->string('surname');
        $table->string('idNumber');
        $table->string('phone');
        $table->string('email');
        $table->unsignedInteger('suburb_id');
        $table->timestamps();


        $table->index('suburb_id');
    });

这是我尝试过的代码

$tests =  DB::table('tests')
                ->select('RequestID','bookingDate','timeSlot','name','surname')
                ->join('nurses','nurses.nurseID','test_bookings.nurse_id')
                ->join('test_bookings','test_bookings.RequestID','=','tests.RequestID')
                ->get();

【问题讨论】:

  • 您在使用该代码时遇到了什么问题?
  • 我的表格没有显示数据。如果我从 test_bookings 中选择并加入护士表,则会显示数据,但是当我加入测试表时,什么也没有显示。因此,我尝试了上面的代码,但仍然没有显示

标签: laravel laravel-5


【解决方案1】:

但是当我加入测试表时,什么都没有显示

因为你使用join 子句来生成innerJoin 语句,要查看结果你应该使用leftJoin

$tests =  DB::table('tests')
                ->select('RequestID','bookingDate','timeSlot','name','surname')
                ->leftJoin('nurses','nurses.nurseID','=','test_bookings.nurse_id')
                ->leftJoin('test_bookings','test_bookings.RequestID','=','tests.RequestID')
                ->get();

【讨论】:

  • 非常感谢,一切正常
【解决方案2】:

为什么你不在这里使用 ORM,一个简单的一对一关系可以完美地完成这项工作。这是一个例子:

class TestBooking extends Model {
 # Other code...
 public function nurse(){
    return $this->belongsTo(Nurse::class);
  }
}
class Test extends Model {
 # Other code...
 public function testBooking(){
    return $this->belongsTo(TestBooking::class, 'RequestID','RequestID');
  }
}

现在您可以像这样获取所有数据:

 $tests = Test::with("testBooking","testBooking.nurse")->get();
 // and get data inside loop like this:
 $test->RequestID // to get request ID
 $test->testBooking->bookingDate // to get booking date
 $test->testBooking->timeSlot // to get timeSlot
 $test->testBooking->nurse->name // to get nurse name
 $test->testBooking->nurse->surname // to get nurse surename

了解更多关系read documention

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多