使用Eloquent删除包含文件的许多项目

时间:2017-06-14 07:25:09

标签: laravel

我有#include <stdio.h> #include <stddef.h> #include <limits.h> #if CHAR_BIT != 8 #error "unsupported char size" #endif int isLittleEndian () { static const int num = 1; if (1 == *((char *) &num)) { return 1; } else { return 0; } } char * getBinaryHostMemoryRepresentation (char * const buffer, size_t bufSize, const void * var, size_t varSize) { size_t byteIdx; size_t bitIdx; if (bufSize < varSize * CHAR_BIT + 1) { return NULL; } const unsigned char * curByte = (const unsigned char *) var; for (byteIdx = 0; byteIdx < varSize; ++byteIdx, ++curByte) { for (bitIdx = 0; bitIdx < CHAR_BIT; ++bitIdx) { unsigned char curBit = (*curByte & (1 << ((CHAR_BIT - 1) - bitIdx))) >> ((CHAR_BIT - 1) - bitIdx); buffer[byteIdx * CHAR_BIT + bitIdx] = curBit + '0'; } } buffer[varSize * CHAR_BIT] = '\0'; return buffer; } char * getBinaryRepresentation (char * const buffer, size_t bufSize, const void * var, size_t varSize) { size_t byteIdx; size_t bitIdx; if (bufSize < varSize * CHAR_BIT + 1) { return NULL; } const unsigned char * curByte;; int incByte; if (isLittleEndian ()) { curByte = (const unsigned char *) var + (varSize - 1); incByte = -1; } else { curByte = (const unsigned char *) var; incByte = 1; } for (byteIdx = 0; byteIdx < varSize; ++byteIdx, curByte += incByte) { for (bitIdx = 0; bitIdx < CHAR_BIT; ++bitIdx) { unsigned char curBit = (*curByte & (1 << ((CHAR_BIT - 1) - bitIdx))) >> ((CHAR_BIT - 1) - bitIdx); buffer[byteIdx * CHAR_BIT + bitIdx] = curBit + '0'; } } buffer[varSize * CHAR_BIT] = '\0'; return buffer; } int main () { int integer = 2882400235; /* 10101011110011011110111111101011 */ char bufferMemInt[sizeof (integer) * CHAR_BIT + 1]; char bufferBinInt[sizeof (integer) * CHAR_BIT + 1]; printf ("integer 2882400235:\n"); if (getBinaryHostMemoryRepresentation (bufferMemInt, sizeof (bufferMemInt), (void *) &integer, sizeof (integer))) { printf ("Host memory binary representation: \"%s\"", bufferMemInt); printf ("\n"); } if (getBinaryRepresentation (bufferBinInt, sizeof (bufferBinInt), (void *) &integer, sizeof (integer))) { printf ("Human readable binary representation: \"%s\"", bufferBinInt); printf ("\n"); } float floating = 42.0; /* 01000010001010000000000000000000 */ char bufferMemFloat[sizeof (floating) * CHAR_BIT + 1]; char bufferBinFloat[sizeof (floating) * CHAR_BIT + 1]; printf ("\n"); printf ("single floating point 42.0:\n"); if (getBinaryHostMemoryRepresentation (bufferMemFloat, sizeof (bufferMemFloat), (void *) &floating, sizeof (floating))) { printf ("Host memory binary representation: \"%s\"", bufferMemFloat); printf ("\n"); } if (getBinaryRepresentation (bufferBinFloat, sizeof (bufferBinFloat), (void *) &floating, sizeof (floating))) { printf ("Human readable binary representation: \"%s\"", bufferBinFloat); printf ("\n"); } return 0; } ,其中包含许多Ad

AdPhoto

它可以直接删除AdPhoto(删除db记录和文件):

// Ad model
public function ad_photos() {
  return $this->hasMany('App\AdPhoto')->orderBy('position', 'desc');
}
// AdPhoto model
public static function boot() {
  parent::boot();
  AdPhoto::deleting(function($photo) {      
    \File::delete(public_path('photos').'/'.$photo['filename']);    
  }
}

它无法删除广告(不删除文件,只删除数据库记录):

\App\AdPhoto::find($id)->delete();

如何让它发挥作用?

(Laravel 5.4)

3 个答案:

答案 0 :(得分:0)

您可以在AdPhoto::deletePhotos($adId); 模型中创建方法并使用它而不是使用事件:

public static function deletePhotos($adId)
{
    $photos = this->where('ad_id', $adId)->get();
    foreach ($photos as $photo) {
        \File::delete(public_path('photos').'/'.$photo['filename']);
    }
    return $this->destroy($photos->pluck('id')->toArray());
}

方法示例:

<XRF r="9.0.2" c="iD2" g="" u="SG31378-narnapid01" k="969232067" 
d="20170613" t="111536">
<IL k="1">
<I k="86103141,812,333">
<M k="6" b="62409" c="333"/>
<M k="186" b="62409" v="543014"/>
<P k="3,1,1" s="1" td="10" v="9617.5" t="190350" d="20170613" z="1" n="273" 
 b="635"/>
<P k="33,537,1" s="1" td="10" v="-14" t="190350" d="20170613" z="1" n="273" 
b="635"/>
<P k="33,579,1" s="1" td="10" v="-0.145356" t="190350" d="20170613" n="273" 
b="635"/>
<P k="12,0,1" vt="1" s="1" td="10" v="9617.5" t="190350" d="20170613" z="1" 
n="273" b="635"/>
</I>

答案 1 :(得分:0)

尝试创建Event Observer以收听删除事件。

所以你的AdPhoto观察者会有这个函数来处理删除。

public function deleting(\App\AdPhoto $photo)
{
    // remove from storage here
    \File::delete(public_path('photos').'/'.$photo->filename); 
}

答案 2 :(得分:0)

解决了这个问题:

foreach($ad->ad_photos()->get() as $photo) {
    $photo->delete();
}