用了100%的腦域試著空想排版你的源碼後:
你不能直接對著 all()
call Model
裡面定義的方法
// 因為ReservationDetail::all()回傳的是 Collection
ReservationDetail::all()->belongsToReservation()
如果要預先載入關聯的話請用 wtih
,with
裡面要打你關聯用的 method
,所以整個來講應該是這樣
Reservation
class Reservation extends Model {
protected $table = 'dbo.Reservation';
public function reservationDetails() {
return $this->hasMany('App\Models\ReservationDetail', 'ReservationID', 'ReservationID');
}
}
ReservationDetail
class ReservationDetail extends Model {
protected $table = 'dbo.ReservationDetail';
public function reservation() {
return $this->belongsTo('App\Models\Reservation', 'ReservationID', 'ReservationDetailID');
}
}
ReservationController
public function showByReservationNo($reservation_no) {
$reservationDetails = ReservationDetail::with('reservation')
->where('ReservationNo', '=', $reservation_no)
->get();
return $reservationDetails;
}