在我的Laravel 5.7应用程序中,我有2个表Tag,TagDetail(一对一关系),而第二个表已将图像上载到存储和图像字段。 我想使用启动方法自动删除相关行和图像。结果,删除与标签行相关的TagDetail,但是TagDetail的图像被删除 未删除。 我有2个模型和新的Tag())-> d(只是调试功能 app / Tag.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use DB;
use App\MyAppModel;
use App\TagDetail;
use App\Http\Traits\funcsTrait;
use Illuminate\Validation\Rule;
use App\Rules\TagUniqueness;
class Tag extends MyAppModel
{
use funcsTrait;
protected $table = 'tags';
protected $primaryKey = 'id';
public $timestamps = false;
private $votes_tag_type= 'votesTagType';
public function getTableName() : string
{
return $this->table;
}
public function getPrimaryKey() : string
{
return $this->primaryKey;
}
public function tagDetail()
{
return $this->hasOne('App\TagDetail', 'tag_id', 'id');
}
protected static function boot() {
parent::boot();
static::deleting(function($tag) {
with (new Tag())->d( '<pre>Tag BOOT $tag::' . $tag->id);
$relatedTagDetail= $tag->tagDetail();
if ( !empty($relatedTagDetail) ) {
$relatedTagDetail->delete(); // I see this is triggered and relatedTagDetail is deleted
}
});
}
和app / TagDetail.php:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use DB;
use App\MyAppModel;
use App\library\ImagePreviewSize;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use App\Http\Traits\funcsTrait;
class TagDetail extends MyAppModel
{
use Notifiable;
use funcsTrait;
protected $table = 'tag_details';
protected $primaryKey = 'id';
public $timestamps = false;
protected $fillable = [
'tag_id',
'image',
'description',
];
public function getTableName() : string
{
return $this->table;
}
public function getPrimaryKey() : string
{
return $this->primaryKey;
}
public function Tag()
{
return $this->belongsTo('App\Tag', 'tag_id');
}
protected static function boot() {
parent::boot();
static::deleting(function($tagDetail) { // THIS METHOD IS NOT TRIGGERED AT ALL!
with (new TagDetail())->d( '<pre>TagDetail BOOT $tagDetail::' . $tagDetail->id);
$tag_detail_image_path= TagDetail::getTagDetailImagePath($tagDetail->id, $tagDetail->image, true);
with (new TagDetail())->d( '<pre>TagDetail BOOT $tag_detail_image_path::' . $tag_detail_image_path);
TagDetail::deleteFileByPath($tag_detail_image_path, true);
});
}
我的模型声明有问题吗?
已修改的块#2: 在我包含的文件public / js / defaultBS41Backend / admin / tag.js中,我有方法:
backendTag.prototype.deleteTag = function (id, name) {
confirmMsg('Do you want to delete "' + name + '" tag with all related data ?', function () {
var href = this_backend_home_url + "/admin/tag/destroy";
$.ajax({
type: "DELETE",
dataType: "json",
url: href,
data: {"id": id, "_token": this_csrf_token},
success: function (response) {
$("#btn_run_search").click()
},
error: function (error) {
alertMsg(error.responseJSON.message, 'Tag deleting error!', 'OK', 'fa fa-exclamation-triangle')
}
});
}
);
} // backendTag.prototype.deleteTag = function ( id, name ) {
并处于控制状态:
public function destroy(Request $request)
{
$id = $request->get('id');
$tag = MyTag::find($id);
if ($tag == null) {
return response()->json(['error_code' => 11, 'message' => 'Tag # "' . $id . '" not found!', 'tag' => null],
HTTP_RESPONSE_INTERNAL_SERVER_ERROR); //500
}
DB::beginTransaction();
try {
$tag->delete();
DB::commit();
} catch (Exception $e) {
DB::rollBack();
return response()->json(['error_code' => 1, 'message' => $e->getMessage(), 'tag' => null], HTTP_RESPONSE_INTERNAL_SERVER_ERROR);
}
return response()->json(['error_code' => 0, 'message' => ''], HTTP_RESPONSE_OK_RESOURCE_DELETED); // 204
} // public function delete(Request $request)
和routes / web.php中:
Route::group(['middleware' => ['auth', 'isVerified'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
Route::delete('/tag/destroy', 'Admin\TagsController@destroy');
...
答案 0 :(得分:0)
问题是$tag->tagDetail()
:您正在使用查询生成器并直接在数据库中删除模型。但是deleting
事件只能在您首先检索模型时触发。
将$relatedTagDetail = $tag->tagDetail();
替换为$relatedTagDetail= $tag->tagDetail;
。