suppose I have $data
, which is a collection of entire data of a table, is it possible to pass this collection type data in parameters via URL from view to controller?
I'm using Laravel. All the solutions I've been searching only for array type data. Thanks for help!
You can. First install the package guzzlehttp/guzzle
. then try this way:
use GuzzleHttp\Client;
$client = new Client();
$sampleData = ['name' => 'billy', 'email' => 'billy@example.com']; // your collection
$url = 'http://api.example.com/bla-bla'; // your url
$res = $client->request('POST', "{$url}",['form_params' => $sampleData]);
$data = json_decode(json_encode($res->getBody()->getContents()),true);
return $data;
Make a post request from your view to the required URL.
Example: Adapted from here
<table id="tData">
<tbody>
<tr>
<td class='dataVal1'>100</td>
...
$(document).ready(function() {
var toServer = {};
var data = $('#tData tbody tr td').each(function(key, value) {
toServer[$(this).attr('id')] = $(this).text();
});
$.ajax({
url: '/test/',
data: {
"_token": "{{ csrf_token() }}",
"table_data": toServer,
}
type: 'POST'
})
});
Now in your controller which handles the page, use the following
public function test(Request $request)
{
dd($request);
}
Note: Make sure the URL which you mention in the ajax request can accept post request.