'Laravel pass id to controller using AJAX
Route
Route::post('approve', 'PostsController@approve');
javascript
$(document).ready(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$('.btn-approve-post').on('click', function(){
var $btn = $(this);
var post_id = $(this).closest('.post').data('post-id'); // it's a number like 6 or 7 or so on.
$btn.prop('disabled', true);
$.ajax({
type: 'post',
url: 'approve',
data: {'id' : post_id},
dataType: 'json',
success: function(response){
$btn.prop('disabled', false);
console.log(111111);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
});
});
method in Controller
class PostsController extends Controller {
public function approve($id)
{
DB::table('posts')
->where('id', $id)
->update(['is_approved' => 1]);
}
}
but when I try to pass id this way, it doesn't work. How to make it work? And what type of response should give back my method in this case?
Solution 1:[1]
Arguments in the controller functions in Laravel are parameters that are in the url, defined in the routes. To get the post data, you need to use the $request
variables.
Your code should be something like this:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostsController extends Controller {
public function approve(Request $request)
{
DB::table('posts')
->where('id', $request->id)
->update(['is_approved' => 1]);
}
}
More information about request variables can be found in the documentation.
Solution 2:[2]
There are many methods to get the post parameters in the controller function as:
Method 1;
Use id as a wild card parameter in your route as
Route::post('approve/{id}', 'PostsController@approve');
In your ajax function you can get it as:
$.ajax({
type: 'post',
url: 'approve/'+post_id,
dataType: 'json',
success: function(response){
$btn.prop('disabled', false);
console.log(111111);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
In your controller function:
public function approve($id)
{
DB::table('posts')
->where('id', $id)
->update(['is_approved' => 1]);
}
Method 2 use request method:
In Route :
Route::post('approve', 'PostsController@approve');
In Ajax Call:
$.ajax({
type: 'post',
url: 'approve',
data: {'id' : post_id},
dataType: 'json',
success: function(response){
$btn.prop('disabled', false);
console.log(111111);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
}
});
In controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostsController extends Controller {
public function approve(Request $request)
{
DB::table('posts')
->where('id', $request->id)
->update(['is_approved' => 1]);
}
}
Solution 3:[3]
You didn't use id
as a wild card parameter in your route. So try the following Code:
use Illuminate\Http\Request;
class PostsController extends Controller {
public function approve(Request $request)
{
$id = $request->get('id');
DB::table('posts')
->where('id', $id)
->update(['is_approved' => 1]);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Sagar Arora |
Solution 3 |