2014-10-20 174 views
1

你好我想,當發生以下情況來更新我的tagtask表(數據透視表)中插入一行:刪除行和數據透視表laravel

因此,例如,我們已經擁有了有3個標籤與它的任務例如市場營銷,開發,會計。現在我們要編輯該任務並刪除這3個標籤並添加一個名爲cooking的標籤。

所以我想到的是從tagtask表中刪除這3個標籤,並在tagtask表中添加標籤'cooking' 。此外,我並沒有從標籤表中刪除標籤,因爲有些任務可能使用市場營銷或開發或會計標籤。

不幸的是關於第一部分是我腦子裏想我不能意識到這一點。要告訴你我已經盡力了,我會第一時間告訴你一件我的觀點的代碼:

{{Form::model($task,array('route'=>array('user.tasks.update', $task->id), 'method'=>'put'))}} 
    <ul> 
     <li> 
       {{Form::label('tag', 'tags')}} 
       <input type="text" name="tag_name" class="form-control" value='@foreach($task->tagtask as $tt){{$tt->tag['tag_name']}} @endforeach'> 
     </li> 
     <li> 
       {{Form::submit('Edit Task', array('class' => 'btn btn-default'))}} 
     </li> 
    </ul> 
{{Form::close()}} 

這裏是我的TaskController.php用下面的代碼:

所以我使用該代碼取得的成就是,我可以刪除視圖中的3個標籤(如前所述),並添加一個新標籤進行更新。然後將新標籤存儲在tagtask表和標籤表中,但問題是舊的3個標籤仍然位於tagtask表中。雖然他們應該被刪除..有人可以幫助我嗎?很高興我在等待你的回答。無論如何感謝您的回答。

回答

0

我對那些有興趣誰解決

public function update($id) 
{ 
    $task = Task::findOrFail($id); 
    $str = Input::get('tag_name'); 
    //Split string by a regular expression 
    $arrayTags = preg_split('/[, ;]/', $str); 

    $tagIds = []; 
    foreach ($arrayTags as $tag) { 
     // here you can apply trim or skipping empty tags 
     $fTag = Tag::firstOrCreate(['tag_name' => $tag]); 
     $tagIds[] = $fTag->id; 
    } 

    $task->tags()->sync($tagIds); 

    return Redirect::route('user.tasks.index'); 
} 
當然

。 TaskController.php中只做了一些更改。下面是評論的代碼,這樣就可以更容易理解:

<?php 
public function update($id) 
    { 
     $str = Input::get('tag_name'); 
     //Split string by a regular expression 
     $inputTags = preg_split('/[, ;]/', $str); 

     $tt_current = Tagtask::where('id_task', $id)->get(); 
     $oldTaskTagsToDestroy = array(); 

     $tags_raw = Tag::all(); 
     $tags = array(); 

     foreach($tt_current as $item) 
     { //id_tag is the key with the tagtaskID as value. 
      $oldTaskTagsToDestroy[$item->id_tag] = $item->id; 
     } 

     foreach($tags_raw as $tag) 
     { //tag_name is the key with the tagID as value. 
      $tags[$tag->tag_name] = $tag->id; 
     } 

     foreach($inputTags as $tag) 
     { 
      if(!isset($tags[$tag])) 
       /* At first in $tag lives the name of the tag that was given through 
       input from a user. After that we return the id through the insertGetId() method and 
       put in $tags[$tag]*/ 
       $tags[$tag] = DB::table('tags')->insertGetId(
        array('tag_name' => $tag) 
       ); 

       //for example tagtask ID 40 exists, so we don't execute the body of the if statement 
      if(!isset($oldTaskTagsToDestroy[ $tags[$tag] ])){ 

       $data_TagTask= array(
         'id_task' => $id, 
         'id_tag' => $tags[$tag] 
         ); 

       DB::table('tagtasks')->insertGetId($data_TagTask); 

      } else 
       /*So we remove the key and the value of tagtask ID 40, because it's again present in the new input. 
       We must do this, so that the old tasktags ID's will not be deleted from the tagtask table.*/ 
       unset($oldTaskTagsToDestroy[ $tags[$tag] ]); 
     } 
     //And here we remove old tagtask ID's that aren't present anymore/again with the new input. So we delete them from the database. 
     foreach($oldTaskTagsToDestroy as $key => $value) 
     { 
       Tagtask::destroy($value); 
     } 

     return Redirect::route('user.tasks.index'); 
    } 

要真正說實話,我已經從我的實習導師獲得了幫助。我是一名建立Web應用程序的公司的實習生。我正在盡我所能成爲一名優秀的程序員。

1

您應該使用sync了點。也許我錯過了什麼,但代碼應該是這樣的:你需要有tags關係爲Task模型

+0

嗨,再次感謝您的回答!您或其他人也可以向我展示它的查詢生成器版本嗎?如果問得太多,那我真的不介意。這是因爲我的好奇心,呵呵。我睡了之後會測試你的代碼,因爲此刻我真的很累.. – superkytoz 2014-10-20 22:51:23

+0

@superkytoz如果你使用Eloquent,你應該使用它,因爲做一些任務要容易得多。使用查詢生成器,您可能會得到如此長的代碼作爲您的代碼 – 2014-10-21 05:53:54

+0

當我使用sync()方法時,出現以下錯誤:調用未定義的方法Illuminate \ Database \ Query \ Builder :: sync()我也製作了一個截圖它:http://i.imgur.com/eJ6CPIv.png這是我在我的控制器中使用的代碼:$ task-> tagtask() - > sync($ tagIds);這裏是住在我的模型任務tagtask()方法:公共職能tagtask() \t {\t \t \t //一對多 \t \t回$這個 - >的hasMany( 'Tagtask', 'id_task',「ID 「); \t} – superkytoz 2014-10-21 08:13:23