javascript - Laravel/Angularjs: Insert into pivot table with less queries -


i succeeded @ creating simple "add topic" form test laravel's pivot operations. consists of title, body , tag checkboxes. models post, tag , pivot posttag.

after reading laravel documentation on updating pivot tables, seems me i'm making way many queries create new topic , update pivot's table. also, way pass on checkbox values (tags) seems kind of sloppy me.

here angular controller:

app.controller('newpostcontroller', function($scope, $http) {     $scope.selection = [];     $http.get('/new_post').success(function(tags) {     $scope.tags = tags; });  $scope.toggleselection = function toggleselection(tag) {     var idx = $scope.selection.indexof(tag);      // selected     if (idx > -1) {         $scope.selection.splice(idx, 1);     }      // newly selected     else {         $scope.selection.push(tag);     }     };      $scope.addpost = function() {         $scope.post.selection = $scope.selection;         $http.post('new_post', $scope.post).success(function() {                 });     } 

... , laravel controller:

class postcontroller extends basecontroller {     public function add() {         if($post = post::insertgetid(array('title' => input::json('title'),                                             'body' => input::json('body')))) {             $tags = [];             foreach(input::json('selection') $tag) {                 array_push($tags, $tag['id']);             }             $new_post = post::find($post);             $new_post->tags()->sync($tags);         }     } } 

with i'm making 5 queries achieve final result. however, followed laravel's documentation on this. should use normal query instead?

thanks!!


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -