Php Laravel仅删除相关模型,但不删除主模型
我有一个简单的查询,运行方式如下:Php Laravel仅删除相关模型,但不删除主模型,php,mysql,laravel,Php,Mysql,Laravel,我有一个简单的查询,运行方式如下: $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_star
$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\u unit
中删除相关数据,但不会从主reviews
表中删除
我已启用查询日志,它尝试运行的查询为:
array:1 [
0 => array:3 [
"query" => "delete from `reviews` where `id` = ?"
"bindings" => array:1 [
0 => 287
]
"time" => 0.53
]
]
因此,我尝试从
reviewswhere
id=287
复制这个查询delete,并直接从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');
}
}