In my jquery ajax request
$('#form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: "/ajaxpage",
data:{
selected_data: JSON.stringify(selected_data),
},
dataType : 'json',
success: function( data ) {
console.log(data);
}
});
});
In my controller method im trying to access the array which i have sent from ajax by looping like this
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
class AjaxController extends Controller
{
public function postData(Request $request)
{
foreach ($request['selected_data'] as $data => $value)
{
$user->roles()->attach(role->id, ['pivot_colmumn' => $value]);
}
}
}
I think this is not a proper way. For single variable i use $request['var_name'] and it works but for array it gives 500 internal server error. So do i access the array values ?