lundi 29 octobre 2018

Why deleting row in related row boot( is not trigegred?

In my Laravel 5.7 app I have 2 tables Tag, TagDetail(One to One relation) and the second table has image uploaded to storage and image field. I want using boot method for automatic deletion of related rows and image. As result deleting Tag row related TagDetail is deleted, but image of TagDetail is not deleted. I have 2 models and new Tag())->d( is just debugging function 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 
            }
        });
    }

and 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);
        });
    }

Is something wrong in my models declarations ?

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire