我有一个简单的查询,运行方式为:
$reviews = Review::whereHas('units', function ($q) use ($after_start_unit_ids) {
$q->whereIn('units.id', $after_start_unit_ids);
})
->withCount('units')
->with(['units' => function($q) use($after_start_unit_ids) {
$q->whereIn('id', $after_start_unit_ids);
}])
->where('action',1)
->where('amenity_value_id', $request->av_id)
->where('property_id', $property_id)
->where('status', 1)
->get();
foreach($reviews as $tk => $tv){
if($tv->units_count == $tv->units->count()){
// DB::connection()->enableQueryLog();
Review::where('id', $tv->id)->forceDelete();
// $queries = DB::getQueryLog();
// pe($queries);
} else {
$tv->units()->detach($after_start_unit_ids);
}
}
在这里,行Review::where('id',$tv->id)->forceDelete();
不起作用。令人惊讶的是,它不会产生任何错误。但是,它会从数据透视表review_unit
中删除相关数据,但不会从主reviews
表中删除。
我已启用查询日志,而它试图运行的查询是:
array:1 [
0 => array:3 [
"query" => "delete from `reviews` where `id` = ?"
"bindings" => array:1 [
0 => 287
]
"time" => 0.53
]
]
因此,我尝试通过复制此查询delete from
评论where
id = 287
并直接从phpmyadmin运行它来进行尝试。从PHPmyadmin运行正常。我在这里很失落,甚至对发生的事情一无所知。
此评论表模型如下:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Review extends Model
{
use SoftDeletes;
protected $table = 'reviews';
protected $guarded = ['id'];
protected $dates = ['created_at','updated_at'];
/**===========
* Relations
=============*/
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function property()
{
return $this->belongsTo('App\Models\Property');
}
/**
* Return respected amenity
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function amenityValue()
{
return $this->belongsTo('App\Models\AmenityValue');
}
public function unitAmenityValues()
{
return $this->hasMany(
'App\Models\UnitAmenityValue',
'amenity_value_id',
'amenity_value_id'
);
}
Public function units()
{
return $this->belongsToMany('App\Models\Unit', 'review_unit');
}
public function amenity()
{
return $this->belongsTo('App\Models\Amenity');
}
public function category()
{
return $this->belongsTo('App\Models\Category');
}
}
这是与该reviews
表有直接关系的那些表的屏幕截图