1

i have a sortable table and after successfully moving an item i want to update all the rows in the databasetable which are effected from sorting.

my problem is that i dont know what's the best way to update multiple rows in my database with eloquent and how to send the data correct with angularjs

in angularjs i did this

//creating the array which i want to send to the server
var update = [];
for (min; min <= max; min++){
    ...
    var item = {"id": id, "position": position};    
    update.push(item);
    ...
}
//it doesn't work because its now a string ...
var promise = $http.put("/api/album/category/"+update);

//yeah i can read update in my controller in laraval, but i need the fakeid, because without 
//i get an error back from laravel...  
var promise = $http.put("/api/album/category/fakeid", update); 

in laravel i have this, but is there an possibility to update the table with one call instead of looping

//my route
Route::resource('/api/album/category','CategoryController');

//controller
class CategoryController extends BaseController {
    public function update()
    {
        $updates = Input::all();

        for($i = 0; $i<count($updates); $i++){
            Category::where('id','=', $updates[$i]["id"])
            ->update(array('position' =>  $updates[$i]["position"]));
        }
    }
}

and yes this works but i think there are better ways to solve the put request with the fakeid and the loop in my controller ;)

update k routing is solved ;) i just added an extra route

//angularjs
var promise = $http.put("/api/album/category/positionUpdate", update);
//laravel
Route::put('/api/album/category/positionUpdate','CategoryController@positionUpdate');
Gregor Voinov
  • 2,203
  • 7
  • 34
  • 52

1 Answers1

-1

Try post instead put. var promise = $http.post("/api/album/category/fakeid", update);

PUT vs POST in REST

PUT implies putting a resource - completely replacing whatever is available at the given URL with a different thing. By definition, a PUT is idempotent. Do it as many times as you like, and the result is the same. x=5 is idempotent. You can PUT a resource whether it previously exists, or not (eg, to Create, or to Update)!

POST updates a resource, adds a subsidiary resource, or causes a change. A POST is not idempotent, in the way that x++ is not idempotent.

By this argument, PUT is for creating when you know the URL of the thing you will create. POST can be used to create when you know the URL of the "factory" or manager for the category of things you want to create.

so:

POST /expense-report or:

PUT /expense-report/10929

I learned via using following

Laravel+Angular+Bootstrap https://github.com/silverbux/laravel-angular-admin

Laravel+Angular+Material https://github.com/jadjoubran/laravel5-angular-material-starter

Hope this help you understand how to utilize bootstrap & angular and speed up your develop by using starter. You will be able to understand how to pass API request to laravel and get callback response.

Community
  • 1
  • 1
Milo Kang
  • 130
  • 8