First Method
'form_params' => $request->all()
Second Method
'form_params' => [
'type_of_task' => $encrypteds,
'email_address' => $emails_encrypteds,
]
Third Method
$response = $http->post(
Config::get('app.SD_TASK_BOARD_MS_BASE_URL') . Config::get('app.SD_TASK_BOARD_MS_UPDATE'),
[
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $access_token
],
// 'form_params' => $request->all() // form_prams means all data send by request
'multipart' => $multipartArray
]
);
In Laravel, there are several ways to send data from client-side controllers to server-side controllers. Some of the common ways are:
HTTP requests: You can send HTTP requests from the client-side controller to the server-side controller using methods like GET, POST, PUT, DELETE, etc. You can use Laravel's built-in HTTP client or any other HTTP client library like Guzzle.
AJAX requests: You can use AJAX requests to send data asynchronously from the client-side controller to the server-side controller without reloading the page. You can use jQuery or any other JavaScript library to make AJAX requests.
WebSockets: WebSockets provide a real-time, bi-directional communication channel between the client and server. You can use Laravel's built-in WebSockets support or any other WebSocket library to send data from the client-side controller to the server-side controller.
Event broadcasting: Laravel's event broadcasting feature allows you to broadcast events from the client-side controller to the server-side controller using web sockets or other broadcasting drivers. You can use Laravel's built-in event broadcasting or any other event broadcasting library.
REST API: You can create a REST API in Laravel and use it to send data from the client-side controller to the server-side controller. You can use Laravel's built-in API support or any other API library like Lumen.
Sure, here are some examples of different ways to send data from client-side controllers to server-side controllers in Laravel:
HTTP Requests
In the client-side controller, you can use Laravel's built-in HTTP client to send a POST request to the server-side controller:
use Illuminate\Support\Facades\Http;
$response = Http::post('http://example.com/api/user', [
'name' => 'John Doe',
'email' => 'john.doe@example.com',
]);
In the server-side controller, you can retrieve the data using the request object:
public function store(Request $request)
{
$name = $request->input('name');
$email = $request->input('email');
// Store the data in the database
}
AJAX Requests
In the client-side controller, you can use jQuery to make an AJAX request to the server-side controller:
$.ajax({
url: '/api/user',
method: 'POST',
data: {
name: 'John Doe',
email: 'john.doe@example.com',
},
success: function(response) {
console.log(response);
}
});
In the server-side controller, you can retrieve the data using the request object:
public function store(Request $request)
{
$name = $request->input('name');
$email = $request->input('email');
// Store the data in the database
}
WebSockets
In the client-side controller, you can use Laravel's built-in Echo library to send data to the server-side controller using WebSockets:
var socket = io('http://example.com:6001');
socket.on('connect', function() {
socket.emit('message', {
name: 'John Doe',
email: 'john.doe@example.com',
});
});
$response = $http->post(
Config::get('app.SD_WEBSITE_RANKING_MS_BASE_URL') . Config::get('app.SD_WEBSITE_RANKING_MS_STORE_URL'),
[
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $access_token
],
'form_params' => $request->all()
]
);
Another Method
if ($validatorResponse['success']) {
Log::info('Calling WebAccessApiController::getWebAccessToken()');
$access_token = $this->getWebAccessToken();
Log::info('Got the access token from WebAccessApiController::getWebAccessToken(). Now creating Web Access!');
Log::info('SD_WEBSITE_ACCESS_MS_BASE_URL . SD_WEBSITE_ACCESS_MS_STORE_URL: ' . Config::get('app.SD_WEBSITE_ACCESS_MS_BASE_URL') . Config::get('app.SD_WEBSITE_ACCESS_MS_STORE_URL'));
$email_address=$input['email_address'];
$website=$input['website'];
$wizard_project_name=$input['wizard_project_name'];
$user_name=$input['user_name'];
$password=$input['password'];
$tokenid=$input['tokenid'];
$pubskey=$input['pubkey'];
$maintenance_engineer=$input['maintenance_engineer'];
$token_engineer=$input['token_engineer'];
$admin_id=$input['admin_id'];
$admin_email=$input['admin_email'];
$u_org_slugname=$input['u_org_slugname'];
$u_org_organization_id=$input['u_org_organization_id'];
$u_org_role_id=$input['u_org_role_id'];
$http = new Client(); //GuzzleHttp\Client
$response = $http->post(
Config::get('app.SD_WEBSITE_ACCESS_MS_BASE_URL') . Config::get('app.SD_WEBSITE_ACCESS_MS_STORE_URL'),
[
'headers' =>[
'Accept' => 'application/json',
'Authorization' => 'Bearer ' .$access_token
],
'form_params' => [
'type_of_task' => $encrypteds,
'email_address' => $emails_encrypteds,
'website' => $website_encrypteds,
'wizard_project_name' => $project_encrypteds,
'user_name' => $user_encrypteds,
'password' => $pass_encrypteds,
'tokenid' => $tokenid,
'pubkey' => $pubskey,
'maintenance_engineer' => $engineer_encrypteds,
'token_engineer' => $token_engineer,
'admin_id' => $admin_id,
'admin_email' => $admin_email,
'u_org_slugname' => $u_org_slugname,
'u_org_organization_id' => $u_org_organization_id,
'u_org_role_id' => $u_org_role_id,
]
]
);
// Log:info('$http' . $http);
Log::info('Got the response from Task board ');
Log::info('data store ho gaya hai successfully');
$responseJson = json_decode($response->getBody()->getContents(), true);
return response()->json($responseJson, 200);
} else {
return response()->json($responseJson, 422);
}
$response = $http->post(
Config::get('app.SD_TASK_BOARD_MS_BASE_URL') . Config::get('app.SD_TASK_BOARD_MS_STORE_URL')
. '/'
.$project_name,
[
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $access_token
],
// 'form_params' => $request->all()
'multipart' => $multipartArray
]
);
if ($validatorResponse['success']) {
Log::info('Calling TaskboardApiController::getTaskBoardAccessToken()');
$access_token = $this->getTaskBoardAccessToken();
Log::info('Got the access token from TaskboardApiController::getTaskBoardAccessToken(). Now creating Task board!');
Log::info('SD_TASK_BOARD_MS_BASE_URL . SD_TASK_BOARD_MS_STORE_URL: ' . Config::get('app.SD_TASK_BOARD_MS_BASE_URL') . Config::get('app.SD_TASK_BOARD_MS_STORE_URL'));
// document is comming from blade page open
$multipartArray = [];
$resume = null;
if (isset($input['document'])) {
$resume = $input['document'];
}
if (isset($input['document'])) {
log::info('if ke andar aa rh ahai');
$multipartArray[] = [
'name' => 'project_name',
'contents' => $input['project_name']
];
$multipartArray[] = [
'name' => 'weburl',
'contents' => $input['weburl']
];
$multipartArray[] = [
'name' => 'keyword',
'contents' => $input['keyword']
];
$multipartArray[] = [
'name' => 'severity',
'contents' => $input['severity']
];
$multipartArray[] = [
'name' => 'tasktype',
'contents' => $input['tasktype']
];
$multipartArray[] = [
'name' => 'taskdeadline',
'contents' => $input['taskdeadline']
];
$multipartArray[] = [
'name' => 'user',
'contents' => $input['user']
];
$multipartArray[] = [
'name' => 'status',
'contents' => $input['status']
];
$multipartArray[] = [
'name' => 'description',
'contents' => $input['description']
];
$multipartArray[] = [
'name' => 'admin_id',
'contents' => $input['admin_id']
];
$multipartArray[] = [
'name' => 'admin_email',
'contents' => $input['admin_email']
];
$multipartArray[] = [
'name' => 'user_id',
'contents' => $input['user_id']
];
$multipartArray[] = [
'name' => 'user_email',
'contents' => $input['user_email']
];
$multipartArray[] = [
'name' => 'u_org_slugname',
'contents' => $input['u_org_slugname']
];
$multipartArray[] = [
'name' => 'u_org_organization_id',
'contents' => $input['u_org_organization_id']
];
$multipartArray[] = [
'name' => 'u_org_role_id',
'contents' => $input['u_org_role_id']
];
$multipartArray[] = [
'name' => 'url_id',
'contents' => $input['url_id']
];
$multipartArray[] = [
'name' => 'project_id',
'contents' => $input['project_id']
];
$multipartArray[] = [
'name' => 'title',
'contents' => $input['title']
];
$multipartArray[] = [
'name' => 'document',
'contents' => $input['document']
];
array_push($multipartArray,[
'name' => 'project_name',
'contents' => $input['project_name']
]);
array_push($multipartArray,[
'name' => 'weburl',
'contents' => $input['weburl']
]);
array_push($multipartArray,[
'name' => 'keyword',
'contents' => $input['keyword']
]);
array_push($multipartArray,[
'name' => 'severity',
'contents' => $input['severity']
]);
array_push($multipartArray,[
'name' => 'tasktype',
'contents' => $input['tasktype']
]);
array_push($multipartArray,[
'name' => 'taskdeadline',
'contents' => $input['taskdeadline']
]);
array_push($multipartArray,[
'name' => 'user',
'contents' => $input['user']
]);
array_push($multipartArray,[
'name' => 'status',
'contents' => $input['status']
]);
array_push($multipartArray,[
'name' => 'description',
'contents' => $input['description']
]);
array_push($multipartArray,[
'name' => 'admin_id',
'contents' => $input['admin_id']
]);
array_push($multipartArray,[
'name' => 'admin_email',
'contents' => $input['admin_email']
]);
array_push($multipartArray,[
'name' => 'user_id',
'contents' => $input['user_id']
]);
array_push($multipartArray,[
'name' => 'document', // here name should be db name
'contents' => $input['document'] // here name from blade name
]);
array_push($multipartArray,[
'name' => 'user_email',
'contents' => $input['user_email']
]);
array_push($multipartArray,[
'name' => 'u_org_slugname',
'contents' => $input['u_org_slugname']
]);
array_push($multipartArray,[
'name' => 'u_org_organization_id',
'contents' => $input['u_org_organization_id']
]);
array_push($multipartArray,[
'name' => 'u_org_role_id',
'contents' => $input['u_org_role_id']
]);
array_push($multipartArray,[
'name' => 'url_id',
'contents' => $input['url_id']
]);
array_push($multipartArray,[
'name' => 'project_id',
'contents' => $input['project_id']
]);
array_push($multipartArray,[
'name' => 'title',
'contents' => $input['title']
]);
array_push($multipartArray,[
'name' => 'input_title',
'contents' => $input['input_title']
]);
array_push($multipartArray,[
'name' => 'document',
'contents' => file_get_contents($resume),
'filename' => $resume->getClientOriginalName()
]);
}else{
log::info('else ke andar aa rh ahai');
$multipartArray[] = [
'name' => 'project_name',
'contents' => $input['project_name']
];
$multipartArray[] = [
'name' => 'weburl',
'contents' => $input['weburl']
];
$multipartArray[] = [
'name' => 'keyword',
'contents' => $input['keyword']
];
$multipartArray[] = [
'name' => 'severity',
'contents' => $input['severity']
];
$multipartArray[] = [
'name' => 'tasktype',
'contents' => $input['tasktype']
];
$multipartArray[] = [
'name' => 'taskdeadline',
'contents' => $input['taskdeadline']
];
$multipartArray[] = [
'name' => 'user',
'contents' => $input['user']
];
$multipartArray[] = [
'name' => 'status',
'contents' => $input['status']
];
$multipartArray[] = [
'name' => 'description',
'contents' => $input['description']
];
$multipartArray[] = [
'name' => 'admin_id',
'contents' => $input['admin_id']
];
$multipartArray[] = [
'name' => 'admin_email',
'contents' => $input['admin_email']
];
$multipartArray[] = [
'name' => 'user_id',
'contents' => $input['user_id']
];
$multipartArray[] = [
'name' => 'user_email',
'contents' => $input['user_email']
];
$multipartArray[] = [
'name' => 'u_org_slugname',
'contents' => $input['u_org_slugname']
];
$multipartArray[] = [
'name' => 'u_org_organization_id',
'contents' => $input['u_org_organization_id']
];
$multipartArray[] = [
'name' => 'u_org_role_id',
'contents' => $input['u_org_role_id']
];
$multipartArray[] = [
'name' => 'url_id',
'contents' => $input['url_id']
];
$multipartArray[] = [
'name' => 'project_id',
'contents' => $input['project_id']
];
$multipartArray[] = [
'name' => 'title',
'contents' => $input['title']
];
array_push($multipartArray,[
'name' => 'project_name',
'contents' => $input['project_name']
]);
array_push($multipartArray,[
'name' => 'weburl',
'contents' => $input['weburl']
]);
array_push($multipartArray,[
'name' => 'keyword',
'contents' => $input['keyword']
]);
array_push($multipartArray,[
'name' => 'severity',
'contents' => $input['severity']
]);
array_push($multipartArray,[
'name' => 'tasktype',
'contents' => $input['tasktype']
]);
array_push($multipartArray,[
'name' => 'taskdeadline',
'contents' => $input['taskdeadline']
]);
array_push($multipartArray,[
'name' => 'user',
'contents' => $input['user']
]);
array_push($multipartArray,[
'name' => 'status',
'contents' => $input['status']
]);
array_push($multipartArray,[
'name' => 'description',
'contents' => $input['description']
]);
array_push($multipartArray,[
'name' => 'admin_id',
'contents' => $input['admin_id']
]);
array_push($multipartArray,[
'name' => 'admin_email',
'contents' => $input['admin_email']
]);
array_push($multipartArray,[
'name' => 'user_id',
'contents' => $input['user_id']
]);
array_push($multipartArray,[
'name' => 'user_email',
'contents' => $input['user_email']
]);
array_push($multipartArray,[
'name' => 'u_org_slugname',
'contents' => $input['u_org_slugname']
]);
array_push($multipartArray,[
'name' => 'u_org_organization_id',
'contents' => $input['u_org_organization_id']
]);
array_push($multipartArray,[
'name' => 'u_org_role_id',
'contents' => $input['u_org_role_id']
]);
array_push($multipartArray,[
'name' => 'url_id',
'contents' => $input['url_id']
]);
array_push($multipartArray,[
'name' => 'project_id',
'contents' => $input['project_id']
]);
array_push($multipartArray,[
'name' => 'title',
'contents' => $input['title']
]);
array_push($multipartArray,[
'name' => 'input_title',
'contents' => $input['input_title']
]);
}
// document is comming from blade page close
$http = new Client(); //GuzzleHttp\Client
$response = $http->post(
Config::get('app.SD_TASK_BOARD_MS_BASE_URL') . Config::get('app.SD_TASK_BOARD_MS_STORE_URL')
. '/'
.$project_name,
[
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $access_token
],
// 'form_params' => $request->all()
'multipart' => $multipartArray
]
);
// Log:info('$http' . $http);
Log::info('Got the response from Task board ');
Log::info('data store ho gaya hai successfully');
$responseJson = json_decode($response->getBody()->getContents(), true);
return response()->json($responseJson, 200);
} else {
return response()->json($responseJson, 422);
}
=================================
Using array_merge
array_merge Function:
- The array_merge function is used to merge multiple arrays into a single array.
- It combines the keys and values of arrays, overwriting values from previous arrays with the same keys . Here's an example of using array_merge in Laravel:
$array1 = ['a' => 'apple', 'b' => 'banana'];
$array2 = ['b' => 'blueberry', 'c' => 'cherry'];
$merged = array_merge($array1, $array2);
// $merged will contain ['a' => 'apple', 'b' => 'blueberry', 'c' => 'cherry']
more example
$users = [
['id' => 1, 'name' => 'Alice'],
['id' => 2, 'name' => 'Bob'],
['id' => 3, 'name' => 'Charlie'],
];
$additionalData = [
['id' => 4, 'name' => 'David'],
['id' => 5, 'name' => 'Eve'],
];
// Use array_merge to combine two arrays
$combinedUsers = array_merge($users, $additionalData);
// Use array_map to transform data
$squareIds = array_map(function ($user) {
$user['id'] = $user['id'] * $user['id'];
return $user;
}, $users);
=======================================
Another Example
To add some other request fields to the $addcart object using array_map and array_merge, you can create an array with the additional fields you want to include and then use array_merge to merge it with the data retrieved from the addcart object. Here's how you can do it:
Assuming you have a request with additional fields, you can create an associative array with those fields. For example:
$requestData = [
'field1' => $request->input('field1'),
'field2' => $request->input('field2'),
// Add as many fields as needed
];
Next, you can retrieve the $addcart object and convert it to an array. Assuming that $addcart is an Eloquent model, you can convert it to an array using the toArray method:
$addcart = addcart::where('admin_id', $admin_id)->first();
if ($addcart) {
$addcartData = $addcart->toArray();
} else {
$addcartData = [];
}
Now, you can use array_merge to merge the $addcartData and $requestData arrays, including the additional fields from the request:
$mergedData = array_merge($addcartData, $requestData);
The $mergedData array will contain all the fields from the $addcart object along with the additional fields from the request.
Here's the full code:
$requestData = [
'field1' => $request->input('field1'),
'field2' => $request->input('field2'),
// Add as many fields as needed
];
$addcart = addcart::where('admin_id', $admin_id)->first();
if ($addcart) {
$addcartData = $addcart->toArray();
} else {
$addcartData = [];
}
$mergedData = array_merge($addcartData, $requestData);
Now, the $mergedData array contains all the fields you need, including the ones from the $addcart object and the additional fields from the request.
Another Example
If you want to add some other request fields to each element in the collection retrieved with get() using array_map and array_merge, you can do it like this:
$admin_id = 123; // Replace with the actual admin_id
// Retrieve the addcart collection
$addcart = addcart::where('admin_id', $admin_id)->get();
if ($addcart->isNotEmpty()) {
// Define the additional fields from the request
$additionalFields = [
'field1' => $request->input('field1'),
'field2' => $request->input('field2'),
// Add as many fields as needed
];
// Use array_map to add the additional fields to each element in the collection
$addcart = $addcart->map(function ($item) use ($additionalFields) {
return array_merge($item->toArray(), $additionalFields);
});
}
output
Here's a step-by-step explanation:
1.First, you retrieve the addcart collection by applying a query filter to get the records for a specific admin_id.
2.Check if the collection is not empty (i.e., it contains records). If it's empty, you can skip further processing.
3.Define the additional fields you want to add to each element in the collection. These fields come from the request, and you can adjust the field names and values accordingly.
4.Use the map function on the collection to iterate through each element (record) and apply a callback function.
5.In the callback function, you use array_merge to merge the original data from the database (retrieved with $item->toArray()) and the additional fields defined earlier.
Now, the $addcart collection has been updated with the additional fields from the request for each element. You can use the updated collection in your Laravel application as needed.
=====================Another Practical Example================
public function myorders($orderID)
{
log::info("data here myorders");
log::info($orderID);
$currentURL = url()->current();
log::info($currentURL);
$id = Auth::user()->id;
$user = Paytm::where('order_id', $orderID)->first();
log::info('now myorder' . $user);
log::info($user);
if ($user) {
$user = $user->toArray();
} else {
$user = [];
}
$sharedata = sharedata::where('orders_id', $orderID)->first();
if ($sharedata) {
$sharedata = ['orders_id' => $sharedata->orders_id];
} else {
$sharedata = [];
}
log::info($sharedata);
$users = array_merge($user, $sharedata);
log::info($users);
return view('pages.orderconfirm', compact('users', 'currentURL'));
// return view('pages.myorder', compact('data'));
}
How to retrive the value from header request in server side
In client side my controller function
public function index(Request $request, $id)
{
$input = $request->all();
$userAuthId = $request->header('X-User-Auth-Id');
Log::info('myi id me kya aa rha hai'.$userAuthId);
Log::info("what is coming in the request file" . print_r($input,true));
$userAuthId = $request->input('userAuthId');
Log::info('auth id me kya aa rha hai'.$userAuthId);
Log::info('auth id me kya aa rha hai'.$userAuthId);
// Get all the WebAcces from db
$WebAccess = WebAcces::where('slug_id', $id)->get();
log::info('first webaccess me kyaa aa rha hai' . $WebAccess);
$response = [
'success' => true,
'data' => WebAccesResource::collection($WebAccess),
'message' => 'Manager WebAcces retrieved successfully.',
'count' => count($WebAccess),
];
return response()->json($response, 200);
}
In server side my controller function
public function getWebAccess(Request $request,$id)
{
$userAuthId = $request->userAuthId;
Log::info('auth id me kya aa rha hai'.$userAuthId);
try {
Log::info('SD_WEBSITE_ACCESS_MS_ALL_URL: ' . Config::get('app.SD_WEBSITE_ACCESS_MS_ALL_URL'));
$access_token = $this->getWebAccessToken();
$url = ''
. Config::get('app.SD_WEBSITE_ACCESS_MS_BASE_URL')
. Config::get('app.SD_WEBSITE_ACCESS_MS_ALL_URL')
. '/'
. $id;
Log::info('Got the access token from WebAccessApiController::getWebAccessToken(). Now fetching Web Access Data!');
Log::info('ALL WebAccess URL: ' . $url);
$guzzleClient = new Client(); //GuzzleHttp\Client
$headers = [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $access_token,
'X-User-Auth-Id' => $userAuthId,
];
$params = [
'headers' => $headers,
];
// Log::info('params the Response from SD WebAccess MS'.$params);
// $params = [
// 'headers' => [
// 'Accept' => 'application/json',
// 'Authorization' => 'Bearer ' . $access_token,
// ],
// ];
$response = $guzzleClient->request('GET', $url, $params);
Log::info('Got the Response from SD WebAccess MS');
Log::info('Store hone ke baad index page par value aa raha hai !');
$json = json_decode($response->getBody()->getContents(), true);
Log::info('Number of objects in response: ' . count($json['data']));
return $json;
} catch (\Exception $e) {
Log::info('There was some exception in WebAccessApiController->getWebAccess()');
return $e->getResponse()->getStatusCode() . ':' . $e->getMessage();
}
}
Summary
declare $request->all() as form_params
declare all request input value inside array
using multipart array
Laravel's built-in HTTP client to send a POST request to the server-side controller contain url and data
based on particular input request condition send form_params data
use array merge to send data to server side by merging request->all() and particular calculated data from DB like cart_id
get data from two different table using first then apply array_merge
Top comments (0)