2016-07-22 87 views
1

我有一個表後與模型簡單的應用程序:Laravel 5.2軟刪除不起作用

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 
use SoftDeletes; 
class Post extends Model 
{ 
    protected $table = 'post'; 

    protected $dates = ['deleted_at']; 

    protected $softDelete = true; 

} 

我想使例如軟刪除,我使用途徑只是舉例route.php:

<?php 
use App\Post; 

use Illuminate\Database\Eloquent\SoftDeletes; 
Route::get('/delete', function(){ 
    $post = new Post(); 
    Post::find(12)->delete(); 

}); 

我有一欄 「created_at」 與移民創建:

Schema::table('post', function (Blueprint $table) { 
     $table->softDeletes(); 
    }); 

,但不是增加時間到此列,當我運行該網站時,它將刪除具有選定ID的行。我錯在哪裏?

+0

嘗試移動'使用SoftDeletes;'裏面的類。我認爲文件建議 –

回答

5

您需要使用SoftDeletes特質模型裏面,像這樣:

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 
use Illuminate\Database\Eloquent\SoftDeletes; 

class Post extends Model 
{ 
    use SoftDeletes; 

    protected $table = 'post'; 

    protected $dates = ['deleted_at']; 
} 

現在的你,不應用特點,所以很明顯這是行不通的。

另外你在路由文件中有不必要的代碼片段。它應該是這樣的:

<?php 
use App\Post; 

Route::get('/delete', function(){ 
    Post::find(12)->delete(); 
}); 
+0

這是正確的。我正在輸入相同的東西。 –

+0

非常感謝你:)。 – gdfgdfg