属性 从相关模型中获取时此集合实例上不存在
Property does not exist on this collection instance when fetching from related models
我有这个查询,其中 return 个数据来自 table :
$existData =Booking::where(function ($query)use($start, $end){
$query->whereBetween('start', [
$start, $end
])->orwhereBetween('end', [
$start, $end]);
})->where('status','confirmed')->get();
这个 return 的数据但是当我添加关系到它时
$existData =Booking::where(function ($query)use($start, $end){
$query->whereBetween('start', [
$start, $end
])->orwhereBetween('end', [
$start, $end]);
})->where('status','confirmed')->with(['bookingDetails'=> function ($q) use($data){
$q->where('user_type',$data->type);
}])->get();
它给出错误:
Property [user_type] does not exist on this collection instance.
但我已经在预订模型中创建了关系
public function bookingDetails()
{
return $this->hasMany('App\Models\BookingRoomDetails', 'o_id', 'id');
}
BookingDetailModel :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class BookingRoomDetails extends Model
{
use HasFactory;
}
如果外部查询和内部查询 return 为真,我希望整个查询应该只有 return 数据,例如。 $q->where('user_type',$utype->type);
并且时间范围存在并且已经可以正常工作。
解决此问题的任何解决方案
将查询代码更改为:
use Illuminate\Database\Eloquent\Builder;
$existData = Booking::with(['bookingDetails'=> function ($q) use ($data) {
$q->where('user_type',$data->type);
}])->where(function (Builder $query) use ($start, $end) {
return $query->whereBetween('start', [ $start, $end ])
->orwhereBetween('end', [ $start, $end ]);
})->where('status','confirmed')
->get();
我有这个查询,其中 return 个数据来自 table :
$existData =Booking::where(function ($query)use($start, $end){
$query->whereBetween('start', [
$start, $end
])->orwhereBetween('end', [
$start, $end]);
})->where('status','confirmed')->get();
这个 return 的数据但是当我添加关系到它时
$existData =Booking::where(function ($query)use($start, $end){
$query->whereBetween('start', [
$start, $end
])->orwhereBetween('end', [
$start, $end]);
})->where('status','confirmed')->with(['bookingDetails'=> function ($q) use($data){
$q->where('user_type',$data->type);
}])->get();
它给出错误:
Property [user_type] does not exist on this collection instance.
但我已经在预订模型中创建了关系
public function bookingDetails()
{
return $this->hasMany('App\Models\BookingRoomDetails', 'o_id', 'id');
}
BookingDetailModel :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class BookingRoomDetails extends Model
{
use HasFactory;
}
如果外部查询和内部查询 return 为真,我希望整个查询应该只有 return 数据,例如。 $q->where('user_type',$utype->type);
并且时间范围存在并且已经可以正常工作。
解决此问题的任何解决方案
将查询代码更改为:
use Illuminate\Database\Eloquent\Builder;
$existData = Booking::with(['bookingDetails'=> function ($q) use ($data) {
$q->where('user_type',$data->type);
}])->where(function (Builder $query) use ($start, $end) {
return $query->whereBetween('start', [ $start, $end ])
->orwhereBetween('end', [ $start, $end ]);
})->where('status','confirmed')
->get();