2017-05-13 4 views
1

私はLaravelを新しくしました。Laravel5のソフト削除の関連テーブルレコードにNullを設定するには

私はお互いに関連する2つのテーブルを持っています。

他の関連するテーブルレコードがソフト削除されている場合、関連するテーブルレコードにnullを設定したいとします。

私は他の関連するテーブルレコードを削除する方法を知っていますが、どうやってソフト削除の代わりにヌルを設定できますか?

製品モデル(編集後)

class Product extends Model 
{ 
    use SoftDeletes; 
    protected $dates = ['deleted_at']; 

    public $table = "products"; 

    public function projects() 
    { 

     return $this->hasMany("App\Model\Project"); 

    } 


    public static function boot(){ 

     parent::boot(); 

     static::deleted(function($product) 
     { 

      //set null 
      $product->projects()->setNull(); 

     }); 

    } 

} 

プロジェクトモデル(編集後)

<?php 

    class Project extends Model 
    { 
     use SoftDeletes; 
     protected $dates = ['deleted_at']; 

     public function product() 
     { 
      return $this->belongsTo("App\Model\Product"); 

     } 

     public function setNull() 
     { 

      $this->product_id = NULL; 
      $this->save(); 

     } 

    } 

テーブルスキーマ

 Schema::create('projects', function (Blueprint $table) { 

      $table->increments('id'); 

      $table->string('project_name')->unique("project_name"); 
      $table->integer('project_value')->unsigned(); 


      $table->integer('product_id')->unsigned()->nullable(); 
      $table->foreign('product_id')->references('id')->on('products')->onDelete("set null"); 

      $table->timestamps(); 
      $table->softDeletes(); 

      $table->engine = 'InnoDB'; 

     }); 

ノート

一つのプロジェクトは、一つの製品に属しています。ちょっと変わったようですが、大丈夫です。

エラーメッセージ

製品レコードを削除すると、次のエラーが発生します。プロジェクトのモデルでは

[2017-05-13 17:25:28] local.ERROR: BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::setNull() in /vagrant/door/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2443 
Stack trace: 
#0 /vagrant/door/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1239): Illuminate\Database\Query\Builder->__call('setNull', Array) 
#1 [internal function]: Illuminate\Database\Eloquent\Builder->__call('setNull', Array) 
#2 /vagrant/door/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Relation.php(340): call_user_func_array(Array, Array) 
#3 /vagrant/door/app/Model/Product.php(44): Illuminate\Database\Eloquent\Relations\Relation->__call('setNull', Array) 
#4 /vagrant/door/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(348): App\Model\Product::App\Model\{closure}(Object(App\Model\Product)) 
#5 /vagrant/door/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(199): Illuminate\Events\Dispatcher->Illuminate\Events\{closure}('eloquent.delete...', Array) 
#6 /vagrant/door/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(172): Illuminate\Events\Dispatcher->dispatch('eloquent.delete...', Array, false) 
#7 /vagrant/door/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php(148): Illuminate\Events\Dispatcher->fire('eloquent.delete...', Object(App\Model\Product)) 
#8 /vagrant/door/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(748): Illuminate\Database\Eloquent\Model->fireModelEvent('deleted', false) 
#9 /vagrant/door/app/Http/Controllers/ProductController.php(80): Illuminate\Database\Eloquent\Model->delete() 
#10 [internal function]: App\Http\Controllers\ProductController->destroy(Object(App\Http\Requests\ProductRequest), '1') 
+0

は '$製品 - >プロジェクトのようなブートメソッド内の関数を呼び出す()はNULLにそのフィールドを設定し、プロジェクトのモデルのカスタムメソッドを定義します - >のsetNull () ' –

答えて

2

dissociate()の方法を確認してください。これはrリレーションの外部キーとオブジェクト上のリレーションの両方をソートします。

基本的にはこのような何かをしたい:

static::deleted(function($product) 
{ 
    // remove relation 
    $product->projects()->each(function($project) { 
     $project->product()->dissociate(); 
     $project->save(); 
    }); 
}); 
+0

編集のための@jeffrey thx: – Robert

+0

ありがとうございました!私は今それを試してみましょう! – hytm

+0

私が解離すると、もうnullを設定する必要はありませんか? – hytm

1

public function setNull() 
{ 
$this->field_names= NULL; 
$this->save(); 
} 

が次にブート機能であなたが

$product->projects()->setNull(); 

私はP. S-申し訳ありませんように、このメソッドを呼び出し、などのカスタムメソッドを定義します携帯電話

+0

ご協力いただきありがとうございます! – hytm

+1

外部キーは「product_id」です。この場合、 "$ this-> field_names = NULL"は "$ this-> product_id = NULL"にする必要がありますか? – hytm

+0

@Hayatomoはい。 –

関連する問題