There are two tables in the database, which are one-to-many relationships:
Main table (reservation information): dbo.Reservation
[ReservationID] [INT] IDENTITY(1,1) NOT NULL,
[ReservationNo] [NVARCHAR](24) NOT NULL,
......
Details table (reservation details): dbo.ReservationDetail
[ReservationDetailID] [INT] IDENTITY(1,1) NOT NULL,
[ReservationID] [INT] NOT NULL, --外键
......
The code of Model is as follows:
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');
}
}
Call in an 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;
}
}
The error message returned by Laravel is as follows:
"message": "Method belongsToReservation does not exist.",
"status_code": 500,
What is the reason for this? Is it a namespace problem?
What I hope to achieve is to use a certain conditional association of the main table to query the detailed table and return the data of the detailed table. The query SQL statement is as follows:
SELECT ReservationDetail.*
FROM ReservationDetail
INNER JOIN Reservation ON Reservation.ReservationID = ReservationDetail.ReservationID
WHERE Reservation.ReservationNo = 'xxx'
May I ask you all, what is the problem? How should I write the correct grammar?
Another question is if you want the data returned to be returned by merging the main table and the detailed table into one object, for example:
{
"ReservationID": "1",
"ReservationNo": "201601011000",
"ReservationDetails": [
{
"ReservationDetailID": "1",
},
{
"ReservationDetailID": "2",
}
]
}
How to write related query? Please give me some advice, thank you very much! ! !
过去多啦不再A梦2017-05-16 16:54:40
That’s not how it is used
If you want to bring it out directly when querying, similar to join, then
public function showByReservationNo($reservation_no)
{
$reservation_details = ReservationDetail::where('ReservationNo', '=', $reservation_no)
->with('belongsToReservation')
->get();
return $reservation_details;
}
If you want to use it when getting the results, it is equivalent to using each itemSELECT 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;
}
Reference Manual