2014-10-20 173 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