I am trying to update a field for a user in my db, and I am trying to do it with ajax. I have a resource (fans), and in my FansController I have a function update($id):
I use the ajax call like so:
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);
}
});
The full_name and full_location are getting pulled fine. I am not sure if they are being passed to the function correctly though. The console.logs are logging fine, but they are not showing the correct data in the console (it just prints the entire source).
The update function:
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();
}
The DB is not updating, so I assume the function isn't being called (even though the ajax is showing a success). Anything I'm missing? Thank you.
Goto your terminal/command prompt and type and run following command:
php artisan routes
This will return you something like this (Details about all routes declared in your application):
+--------+---------------------------------------------------------------+---------------+-------------------------------+----------------+---------------+
| 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 | | |
+--------+---------------------------------------------------------------+---------------+-------------------------------+----------------+---------------+
From this result, find out the appropriate url
to use for the method. In this case, for the update
method using PATCH
request method
you may use something like this:
posts/10
Since it's a resource controller, you may use something like following:
$.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);
}
});
So your update
method will be matched because, your update
method looks like this:
public function update($id)
{
//...
}
According to your url
you should have following route (resourceful) declared:
Route::resource('fans', 'FansController');
If you are keeping your JS
code in a seoarate file then you may also check this article, it's about how to send a model object from server to client side (as JavaScript
Object), would be helpful to get the idea about to send the id
from server to client, so you may able to use the id
in your JS
function.
your not passing the $id to the controller function
$fan = Fan::where('id', $id)->first();
same as
$fan = Fan::find($id);
these will not return anything because $id isn't set.
Also you are not posting back any data that designates which Fan you are updating, like an id.
UPDATE:
If you are going to pass the id back through the post:
public function update() {
$id = Input::get('id');
$fan = Fan::find($id);
...
}