I've been working on a in a route of validation for the user in Laravel API to connect to a NLP model in Google Cloud. Making the request go to the API them the API makes a request to a model in Google Cloud. After the prediction is given I pass back to the user.
The problem seems that it takes a certain almost of time to receive a answer from the model so I end up answering a empty JSON to the user.
I wish to ask what is better way to proceed in this situation.
public function request_To_The_Model(Request $request)
{
$data = $request->json()->all();
$response = Http::post(URL, [
$data['sentence']
]);
$body = $response->json();
return response()->json($body);
}
Related
Suppose i need to fetch user money transactions there are 100+ transactions in database and i need to send all the user transactions through the API to a android app, i have idea how to make using GET method but using GET method its not Dynamic.
In API i'm sorting data by 4-5 parameters in API input using post method
And i want to make this API for infinite Scrolling
And i'm using Stored Procedure for getting data
Then How can i achieve Laravel pagination in POST method?
my current response something like this
{
"Transactions"[
{
"name":"food";
"amount":100;
}
]
}
Actually, you can do it in multiple ways but the way I use most is like below
public function getTransaction(Request $request)
{
$transactions = Transactions::where('SOME-Condition', $request->Condition)->paginate(10);
$sorted = $transactions ->sortBy([
['name', 'asc'],
['age', 'desc'],
]);
return $sorted;
}
or you can also do it like this
public function getTransaction(Request $request)
{
$transactions = Transactions::where('SOME-Condition', $request->Condition)
->orderBy('abc')
->orderBy('def')
->paginate(10);
return $sorted;
}
What patterns can I use for 'automatic' resource CRUD operations for given Models in Laravel?
Say I have two models SomeModel and SomeRelatedModel where some_related_model.some_model_id is an FK to SomeModel.
The standard method on the SomeModelController for handling the create POST /api/someModel might look like this:
public function store(Request $request)
{
$user = Auth::guard('api')->user();
$data = $request->get('data');
$data['user_id'] = $user->id;
$someModel = SomeModel::create($data);
// has this request been made with the data for the
// related model? If so create this too.
if($data['relatedModel']){
SomeRelatedModel::create(array_merge(
['some_model_id' => $someModel->id]
$data['relatedModel']
));
}
// has this request been made expecting to get related
// models back in the response? If so load these
if($request->has('with')){
$someModel->load($request->get('with'));
}
return (new PostResource($post))
->toResponse($request)
->setStatusCode(201);
}
This works but is very verbose and for models with a sub-sub relation would need changing further. Similar work will need to be done for the other endpoints for all resources.
Is there a more versatile (or tidy) pattern using out-of-the box classes to get a similar effect?
Have a look at Laravel Orion. Fits your use case.
I need help on my Laravel API,
I have an external API Request written in C#
Method: Post
Data is JSON which is being posted to my Laravel API URL: http://localhost/project/api/company-data
API route:
Route::post('company-data', 'API\APIController#index');
Here is my API Controllers index() function
public function index(Request $request)
{
return var_dump($request->all());
}
My API Call is hitting the function since I was able to capture it on logs,
However it is returning me an empty array?
How do I catch the json data that was posted to my API?
Thank You for your help, my apologies if I cannot explain my concern clearly,
If found the solution,
It is $Request should be
json_decode($request->getContent(), true);
instead of
$request->all()
I develop in this moment two little application with Laravel (two diffrent project).
The first stock of orders in database. This application have an API that returns, via GET, the object order.
Example :
In Postman, I call : http://payment.oo/api/orders/15. My API respond this, and it's ok for me.
{
"id": 15,
"client_name": "Mr Doe",
"price": 330.45,
"created_at": "2018-02-07 14:54:07",
"state_id": 2
}
The code of this function in api.php
Route::post('/orders/{id}', function($id){
$order = Order::find($id);
if($order){
return response()
->json($order);
}
else{
abort(404);
}
});
The second application store also orders and calls the API of the first application to know the status of the payment.
So, in the second application, I stock the ID of payment in database and i call the API of the first application with Guzzle, like this
$order = Order::findOrFail($id);
$client = new Client([
'base_uri' => 'http://payment.oo/api/',
// You can set any number of default request options.
'timeout' => 2.0,
]);
$response = $client->request('GET', 'orders/'.$order->payment_id);
That's when I come across a huge problem that I do not understand.
The response to this API request corresponds to a command order of the database of the second application.
{
"id":15,
"client_name":"Mr Doe",
"created_at":"2018-02-08 15:02:16",
"take_at":null,
"finish_at":null,
"state_id":1,
"delivery_type_id":1,
"user_id":null,
"payment_id":30
}
I use Laragon like development web server
Sorry if I was not clear enough :/
And thanks for your help
It's clear that you defined your route as POST but you're calling it with GET using your Guzzle client request.
Solution: whether change the method to POST in your client call, or in the first project route file make the Route as a GET route.
I am currently creating Rest API for Android Team using Laravel 5.2 and testing the API in RESTCLIENT.
I am just trying to get the form values which are entered in Restclient with POST method.
I have problem with POST method.. I m just not able to get the form field values. My API for GET method works fine.
Kindly guide me.
Thanks
This is how you handle POST request in laravel. Be it 5.2 or 5.4.
Route::post('books', 'BookController#store');
Controller
pubic function store(Request $request)
{
$name = $request->name;
$name = $request->get('name');
$name = request('name'); //global helper
//Your testing with api so you should return to api if you want to see it
return response()->json($name);
}
Then check in Android console if you receive $name in JSON format.