所以我正在使用Laravel 5.4并且已经梳理了stackoverflow和web来解决我的问题。我很接近,但事情仍然不太正确。我得到了大多数有效的结果集,但是它们会被奇怪的错误记录所覆盖。
在网上提出许多建议后,我在我的控制器中执行以下操作:
protected function index(Request $request) {
$orderBy = $request->has('order_by') ? $request->get('order_by') : '';
$query = Booking::sortable();
// Limit result set to just the records for this reseller
// if the user is a reseller and not a full admin
if (!empty(\Auth::user()->reseller_id)){
$query->with(['delivery' => function($query){
$query->where('delivery_option_type', 'reseller');
$query->with(['delivery_option' => function($query){
$query->where('reseller_id', \Auth::user()->reseller_id);
}]);
$query->has('delivery_option');
}]);
}
// Determine what query to run based on the selected booking list
switch ($request->get('type')){
case 'upcoming':
$query->doesntHave('named_devices')
->orWhere('pickup_date', '>=', date('Y-m-d'));
break;
case 'active':
$query->where('pickup_date', '<=', date('Y-m-d'))
->has('named_devices');
break;
case 'completed':
$query->withoutGlobalScope(ActiveBookings::class)->ofState(BookingState::COMPLETE);
break;
default:
break;
}
return $query->orderBy($orderBy)->paginate();
}
预订舱有:
public function delivery()
{
return $this->belongsTo(Delivery::class);
}
交付类有这种方法:
public function delivery_option()
{
return $this->morphTo(); // delivery_option_type is 'reseller'
}
交付选项类具有以下方法:
public function reseller(){
return $this->hasOne(Reseller::class);
}
public function deliveries()
{
return $this->morphMany(Delivery::class, 'delivery_option');
}
在DB的大约50个结果的列表中,我有大约6个不正确的reseller_id。
我注意到上面的代码似乎从具有错误reseller_id的预订中删除了reseller_option,但它没有从结果集中删除Booking。需要删除整个预订。
有什么理由不能正确地减少我的设置吗?还有其他建议吗?