2013-11-20 13 views
10

मेरे पास 2 टेबल, ग्राहक और परियोजनाएं हैं और एक परियोजना एक ग्राहक से जुड़ी है। दोनों ग्राहक और परियोजनाएं अभिलेखीय कारणों के संबंधों को बनाए रखने के लिए मुलायम हटाना लागू करती हैं, भले ही मैं ग्राहक को हटा दूं, फिर भी परियोजना में ग्राहक की जानकारी संलग्न होगी।लैरवेल 4: वाद्य मुलायम deletes और रिश्तों

मेरी समस्या यह है कि जब मैं क्लाइंट को हटाता हूं, तो संदर्भ परियोजना से पहुंच से बाहर हो जाता है और अपवाद फेंकता है। मैं क्या करना चाहता हूं वह क्लाइंट को नरम हटा देता है लेकिन प्रोजेक्ट रिलेशनशिप से क्लाइंट डेटा को बरकरार रखता है।

मेरे ब्लेड कोड इस प्रकार है:

@if ($projects->count()) 
<table class="table table-striped table-bordered"> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>Client</th> 
     </tr> 
    </thead> 

    <tbody> 
     @foreach ($projects as $project) 
      <tr> 
       <td>{{{ $project->name }}}</td> 
       <td>{{{ $project->client->name }}}</td> 
       <td>{{ link_to_route('projects.edit', 'Edit', array($project->id), array('class' => 'btn btn-info')) }}</td> 
       <td> 
        {{ Form::open(array('method' => 'DELETE', 'route' => array('projects.destroy', $project->id))) }} 
         {{ Form::submit('Delete', array('class' => 'btn btn-danger')) }} 
        {{ Form::close() }} 
       </td> 
      </tr> 
     @endforeach 
    </tbody> 
</table> @else There are no projects @endif 

यहाँ माइग्रेशन:

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

     // Table engine 
     $table->engine = 'InnoDB'; 

     // Increments 
     $table->increments('id'); 

     // Relationships 

     // Fields 
     $table->string('name'); 

     // Timestamps 
     $table->timestamps(); 

     // Soft deletes 
     $table->softDeletes(); 

    }); 


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

     // Table engine 
     $table->engine = 'InnoDB'; 

     // Increments 
     $table->increments('id'); 

     // Relationships 
     $table->integer ('client_id'); 

     // Fields 
     $table->string('name'); 

     // Timestamps 
     $table->timestamps(); 

     // Soft deletes 
     $table->softDeletes(); 

     // Indexes 
     $table->index('client_id'); 


    }); 

बहुत धन्यवाद।

+1

क्या आपने ':: withTrashed()' का उपयोग करने का प्रयास किया है? –

+0

क्या आप माइग्रेशन दिखा सकते हैं। आप कैसे और क्या वास्तव में हटाने की कोशिश कर रहे हैं? – carousel

+0

मैं ग्राहक से संबंधित (सॉफ्ट डिलीट) को हटाने की कोशिश कर रहा हूं लेकिन क्लाइंट से संबंधित प्रोजेक्ट को देखते समय क्लाइंट का नाम बरकरार रखता हूं। – Wally

उत्तर

29

मॉडल में संबंध परिभाषित करते समय इसे ट्रेसहेड() विधि का उपयोग करके हल किया गया था।

मूल कोड:

public function client() { 
    return $this->belongsTo('Client'); 
} 

समाधान:

public function client() { 
    return $this->belongsTo('Client')->withTrashed(); 
} 

खुशी है कि करने के लिए बहुत-बहुत धन्यवाद मदद करने के लिए।

+0

मेरे लिए भी काम करता है! – od3n

3

मेरे मामले में मैं वैली प्रस्तावित client को संशोधित नहीं कर सकता, क्योंकि इसका उपयोग अन्य मॉडलों और नियंत्रकों के भीतर किया जा रहा है क्योंकि मैं नहीं चाहता कि यह ग्राहक ->withTrashed() प्राप्त करें।

इस मामले में, यहाँ दो समाधान है मेरा प्रस्ताव है:

निर्दिष्ट ->withTrashed() जब उत्सुक लोड हो रहा है ग्राहक:

$projects = Project::with(['client' => function($query){ $query->withTrashed(); }])->get(); 

या एक नई client कार्यों ->withTrashed()

public function client() { 
    return $this->belongsTo('Client'); 
} 

// The new function 
public function client_with_trash() { 
    return $this->belongsTo('Client')->withTrashed(); 
} 

जब उत्सुक लोड हो रहा है बनाने अब:

$projects = Project::with(['client_with_trash'])->get(); 
संबंधित मुद्दे