在ajax调用中,Laravel 4中的资源中用于更新功能的URL是什么

时间:2014-04-04 22:21:59

标签: php jquery ajax laravel laravel-4

我正在尝试为我的数据库中的用户更新字段,我正在尝试使用ajax。我有一个资源(粉丝),在我的FansController中我有一个函数更新($ id):

我像这样使用ajax调用:

var full_name = $("#user_name").val();
var full_location = $("#user_city").val();

request = $.ajax({ 
          url: "http://url.com/fans/update", 
          type: "post", success:function(data){console.log(data);}, 
          data: {'full_name': full_name, 'full_location': full_location} ,beforeSend: function(data){
            console.log(data);
          } 
  });

full_name和full_location被拉得很好。我不确定他们是否正确地传递给函数。 console.logs记录正常,但它们没有在控制台中显示正确的数据(它只打印整个源)。

更新功能:

public function update($id)
        {

            //get saved info from fan profile
            $full_name = Input::get('full_name');
            $full_location = Input::get('full_location');

            //echo var_dump($full_name);

            $split_name = explode(" ", $full_name);

            $first_name = $split_name[0];

            $i=0;
            foreach ($split_name as $name) {

                if($i > 0) {
                    $last_name = $name[i] + " ";
                }
                $i++;
            }

            $trimmed_last_name = trim($last_name);


            //find user
            $fan = Fan::where('id', '=', $id)->first();

            $fan->first_name = $first_name;
            $fan->last_name = $trimmed_last_name;

            $fan->save();
        }  

数据库没有更新,所以我假设函数没有被调用(即使ajax显示成功)。我失踪了什么?谢谢。

2 个答案:

答案 0 :(得分:5)

转到您的终端/命令提示符,然后键入并运行以下命令:

php artisan routes

这将返回这样的内容(有关应用程序中声明的所有路由的详细信息):

+--------+---------------------------------------------------------------+---------------+-------------------------------+----------------+---------------+
| Domain | URI                          |  Name         |  Action                  | Before Filters | After Filters |
+--------+---------------------------------------------------------------+---------------+-------------------------------+----------------+---------------+
|        | GET posts                    | posts.index   | PostController@index     |                |               |
|        | GET posts/c                  | posts.create  | PostController@create    |                |               |
|        | POST posts                   | posts.store   | PostController@store     |                |               |
|        | GET posts/{posts}            | posts.show    | PostController@show      |                |               |
|        | GET posts/{posts}/edit       | posts.edit    | PostController@edit      |                |               |
|        | PUT posts/{posts}            | posts.update  | PostController@update    |                |               |
|        | PATCH posts/{posts}          |               | PostController@update    |                |               |
|        | DELETE posts/{posts}         | posts.destroy | PostController@destroy   |                |               |
+--------+---------------------------------------------------------------+---------------+-------------------------------+----------------+---------------+

从此结果中,找出适用于该方法的url。在这种情况下,对于使用update请求PATCH的{​​{1}}方法,您可以使用以下内容:

method

由于它是资源控制器,您可以使用以下内容:

posts/10

因此,您的$.ajax({ url: "fans/10", // 10 is user id to update, for example type: "post", data: { full_name:full_name, full_location:full_location, _method:"PATCH" }, success:function(data){ console.log(data); } }); 方法会匹配,因为您的update方法如下所示:

update

根据您的public function update($id) { //... } ,您应该声明以下路线(资源丰富):

url

如果您将Route::resource('fans', 'FansController'); 代码保存在seoarate文件中,那么您还可以检查this article,它是关于如何从服务器向客户端发送模型对象(如{{ 1}}对象),有助于了解将JS从服务器发送到客户端的想法,因此您可以在JavaScript函数中使用id

答案 1 :(得分:0)

你没有将$ id传递给控制器​​函数

$fan = Fan::where('id', $id)->first(); 
same as
$fan = Fan::find($id);

这些不会返回任何内容,因为$ id未设置。

此外,您不会回发任何指定您正在更新哪个粉丝的数据,例如ID。

更新:

如果您要通过帖子传回ID:

public function update() {
    $id  = Input::get('id');
    $fan = Fan::find($id);
    ...
}