請教Laravel eloquent model一對多重關聯關係,提示belongsto方法不存在的原因?
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-16 16:52:40
0
1
1301

資料庫中有兩個表,分別為一對多關係:

主表(預約資訊):dbo.Reservation

    [ReservationID] [INT] IDENTITY(1,1) NOT NULL,
    [ReservationNo] [NVARCHAR](24) NOT NULL,
    ......

明細表(預約明細):dbo.ReservationDetail

#
    [ReservationDetailID] [INT] IDENTITY(1,1) NOT NULL,
    [ReservationID] [INT] NOT NULL,                          --外键
    ......

Model的程式碼如下:

Reservation.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Reservation extends Model
{
    protected  $table = 'dbo.Reservation';

    public function hasManyReservationDetails()
    {
        return $this->hasMany('App\Models\ReservationDetail', 'ReservationID', 'ReservationID');
    }
}

ReservationDetail.php

#
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ReservationDetail extends Model
{
    protected  $table = 'dbo.ReservationDetail';

    public function belongsToReservation()
    {
        return $this->belongsTo('App\Models\Reservation', 'ReservationID', 'ReservationDetailID');
    }

}

在一個API的controller裡面呼叫

<?php

namespace App\Api\Controllers;

use App\Models\Reservation;
use App\Models\ReservationDetail;
use App\Http\Requests;

class ReservationController extends BaseController
{

    public function showByReservationNo($reservation_no)
    {

        $reservation_details = ReservationDetail::all()
            ->belongsToReservation()
            ->where('ReservationNo', '=', $reservation_no)
            ->get();

        return $reservation_details;
    }
}

Laravel回傳的錯誤訊息如下:

  "message": "Method belongsToReservation does not exist.",
  "status_code": 500,

請問這是什麼原因,是不是namespace的問題?

我希望實現的是用主表的某個條件關聯去查詢明細表,傳回明細表的數據,查詢的SQL語句如下:

SELECT ReservationDetail.*
FROM ReservationDetail
INNER JOIN Reservation ON Reservation.ReservationID = ReservationDetail.ReservationID
WHERE Reservation.ReservationNo = 'xxx'

請問各位大牛,問題出在什麼地方?準確的文法該怎麼寫?

另一個問題是,如果希望傳回的資料是將主表和明細表合併成一個物件傳回,例如:

{
  "ReservationID": "1", 
  "ReservationNo": "201601011000", 
  "ReservationDetails": [
    {
      "ReservationDetailID": "1", 
    }, 
    {
      "ReservationDetailID": "2", 
    }
  ]
}

關聯查詢該怎麼寫?請各位大牛賜教,感激涕零! ! !

曾经蜡笔没有小新
曾经蜡笔没有小新

全部回覆(1)
过去多啦不再A梦

不是這麼用的
如果你要在查詢的時候直接帶出去,類似join,那就

    public function showByReservationNo($reservation_no)
    {

        $reservation_details = ReservationDetail::where('ReservationNo', '=', $reservation_no)
            ->with('belongsToReservation')
            ->get();

        return $reservation_details;
    }

如果要在取結果的時候用,就是相當於用的時候每條都SELECT Reservation where Reservation.ReservationID = xxx

    public function showByReservationNo($reservation_no)
    {

        $reservation_details = ReservationDetail::where('ReservationNo', '=', $reservation_no)
            ->get();

        foreach($reservation_details as $reservation_detail){
            $reservation = $reservation_detail->belongsToReservation;
            //todo: something you need
        }
        return $reservation_details;
    }

參考手冊

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板