请教Laravel eloquent model一对多关联关系,提示belongsto方法不存在的原因?

数据库中有两个表,分别为一对多关系:

主表(预约信息):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", 
    }
  ]
}

关联查询应该怎么写?请各位大牛赐教,感激涕零!!!

阅读 6.4k
1 个回答

不是这么用的
如果你要在查询的时候直接带出去,类似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;
    }

参考手册

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏