Angular passing params to Laravel to edit

时间:2015-10-06 08:19:23

标签: javascript php angularjs laravel

I am using laravel 5.1, how can the request accept an id passing by angular via an api call? Here is my code.

angular.module('MyApp')
    .factory('Account', function($http){
        return {
getProductToEdit: function(pid){
                //console.log(pid) ----> return 16
                return $http.get('/api/productedit',pid);
            }
    }   
    });

My backed function:

public function updateProductAPI(Request $request)
    {
         $prodId = $request->all(); ---> return []
         // $request->input('pid'); ---> Object {}

         return response()->json($prodId);
    }

Thanks!!

1 个答案:

答案 0 :(得分:5)

The second object passed into the $http.get function is a config object. You need to set the params attribute on this object to send data with the request.

$http.get('url', { params : { pid : pid } });

You can read more about the config object here.

Now within your controller

public function updateProductAPI(Request $request)
{
    // Get the id
    $id = $request->input('pid');
});

Another way when putting the API together is to make use of route parameters to handle unique identifiers such as ids or slugs.

// The get call
$http.get('url/'+pid).then(function(data) { ... });

// routes.php
Route::get('url/{pid}', 'ProductController@updateProductAPI');

// Controller method
public function updateProductAPI($id, Request $request)
{
    // $id available here
}