Laravel 5 为什么不触发删除相关行boot()中的行?

Laravel 5 为什么不触发删除相关行boot()中的行?,laravel-5,eloquent,Laravel 5,Eloquent,在我的Laravel 5.7应用程序中,我有两个表Tag、TagDetail(一对一关系),第二个表的图像上载到存储和图像字段。 我想使用引导方法自动删除相关行和图像。因此,删除与标记行相关的标记详细信息会被删除,但标记详细信息的图像会被删除 未删除。 我有2个型号和新的标签())->d(只是调试功能 app/Tag.php: <?php namespace App; use Illuminate\Database\Eloquent\Model; use DB; use App\My

在我的Laravel 5.7应用程序中,我有两个表Tag、TagDetail(一对一关系),第二个表的图像上载到存储和图像字段。 我想使用引导方法自动删除相关行和图像。因此,删除与标记行相关的标记详细信息会被删除,但标记详细信息的图像会被删除 未删除。 我有2个型号和新的标签())->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 
            }
        });
    }
在控制中:

在routes/web.php中:


问题是
$tag->tagDetail()
:您正在使用查询生成器并直接在数据库中删除模型。但是只有在首先检索模型时才能触发
删除事件


$relatedTagDetail=$tag->tagDetail();
替换为
$relatedTagDetail=$tag->tagDetail;

如何删除模型?请查看原始postTry的修改块2替换
$relatedTagDetail=$tag->tagDetail())
$relatedTagDetail=$tag->tagDetail;
。谢谢!这是一个决定,但是你能解释一下我定义的方法$tag->tagDetail()为什么我要将它称为属性吗?
<?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);
        });
    }
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)
Route::group(['middleware' => ['auth', 'isVerified'], 'prefix' => 'admin', 'as' => 'admin.'], function () {
    Route::delete('/tag/destroy', 'Admin\TagsController@destroy');
    ...