2017-12-26 25 views
0

私にはlocationのビューがあります。このlocationは、異なるservicesを持っています。それらはモデルLocationServiceのモデル(n-m関係)の多くを持つようにリンクされています。別のモデルを同期

このビューでは、locationserviceごとに異なるtagsを選択することができます。私はlocation_service_tagというモデルを持っていて、それはlocation_serviceというIDとtagsというIDを持っています。

今度はシンクメソッドを使用してlocation_serviceの組み合わせのtagsを保存します。この情報をどのように保存することができますか?ここ

(IDは選択自由である)の例である:

私はこの場所は、ID 11を有する2 servicesを有するlocation 1とのビューを持っている、22 彼らが有するlocation_serviceに格納されていますids 111,122。location_service111はタグID1111,2222を有し、location_service122はタグ2222,4444を有する。

ここで、syncを使用してlocation_service_tagにこれらのタグを保存します。これはどのように可能ですか?

これはどういうことかと思いましたが、そうではありません。

foreach($request->servicetags as $servicetag){ 
    if(count($servicetag)>0){ 
     //Final statement must be true, because we want to override e.g. if one is deleted or inserted 
     $location->locationservice()->tags()->sync($servicetag, true); 
    }else{ 
     //There are no services, submit an empty array 
     $location->locationservice()->tags()->sync([], true); 
    } 
    } 

答えて

0

最後に私はそれをした:

//Save relational Data of services 
    //If there are services, add the, else add empty array 
    if(isset($request->locationservices)){ 
    foreach($request->locationservices as $key => $locationservice){ 
     if(count($locationservice)>0){ 
     LocationService::find($key)->tags()->sync($locationservice, true); 
     }else{ 
     LocationService::find($key)->tags()->sync([], true); 
     } 
    } 
    } 

キーがキーとしてlocationservice_idを有し、ループの値は、各locationserviceの配列として選択tag_idsを有しています。

関連する問題