Im currently working on a Laravel APP and one of the endpoints is a method that receives an Axios Post Request from a React APP with some parameters.
On laravel i only need to get those Post Resquest Parameters to save them on a database but im not figure how i get only the values.
Im using Postman to test it and im sending on the body a value '1234567890' with the value 'param', something like this:
axios.post("/myendpoint", {
param: '1234567890',
});
and on Laravel im doing this (as i've seen on another stackoverflow question):
public function myendpointmethod(Request $request)
{
return response()->json($request->param);
}
but this only returns me an empty array.
Im using Laravel Framework 7.25.0.
Thanks a lot
Related
I am using Laravel with Vuejs and AXIOS for HTTP requests. I am sending a post request with the array of objects. So in my laravel store function how can I retrieve that data from the $request?
my questions array looks like:
data:{
questions:[{
question:'',
opt1:''
},
{
question:'',
opt1:''
}
]
}
Laravel Store method in controller:
public function store(Request $request)
{
return $request;
}
vue code:
axios.post('/addTest',this.$data.questions).then(response=>{
console.log(response.data);
});
in this code questions is an array of objects.
In Laravel you have a store method and then you returning the request?Why you are doing that?To see the request from frontend? If so then I recommend you to use postman for this.
Postman is easy to use and you can send a similar request that frontend sends.Then in laravel store function you can do
dd($request) //to output the request that postman sends
You said: how can I retrieve that data from the $request
If you send from frontend something like
{ id: 1 }
Then in laravel you can do
$id = $request->get('id');
Below you can see how i send a request with postman,and how output the request.
Your request with postman
Laravel code to output request
The response from Laravel displayed in postman
If the this.$data.questions is an array, you can simply use the input() method to pull all of the questions:
$questions = $request->input();
Let say you only want to pull the question property of the second item, you can do it like so in Laravel:
$secondQuestion = $request->input('1.question');
However, it would also be nice if you pass the questions as an object:
axios.post('/addTest', { questions: this.$data.questions });
And your PHP part will look like this:
$questions = $request->input('questions');
Hope this gives you some ideas.
I have a laravel api controller where I'm getting data from a table.
public function moveData() {
$movelivesales = DB::table('st_sales_live')->get();
return $movelivesales;
}
I'm getting all the data, now how can I send this data to another project api(this api is ready) using request ? Any help please?
Just create a route like this to get your data($movelivesales):
Route::get('/your/path', 'YourController#moveData');
Then if you access:
http://yourdomain.com/your/path you will get a json of your data
You can get the data from api with php: file_get_contents('http://yourdomain.com/your/path');
Imagine you have the following resources for example: Users, Posts and Comments (With typical relationship setup in laravel).
When fetching a single Post, you will have the following endpoint
GET /api/posts/1
// With route model binding
public function show(Post $post)
{
return $post;
}
This is fine if I only want the Post object, but in some pages in my application, I also need to load the User and the Comments associated with the Post.
How do you guys handle that kind of scenario?
1. Should I load everything in that endpoint like:
return $post->load(['user', 'comments.user']);
and call it a day? (nope)
2. Should I accept an additional parameter that will tell my controller to load the relationship based on that value?
// With route model binding
public function show(Request $request, Post $post)
{
// rel for "relationship"
if ($request->has('rel')) {
$post->load($request->input('rel'));
}
return $post;
}
with this approach I could do something like this:
GET /api/posts/1?rel=user
returns Post with User
or I could build an array of parameter with jquery's $.param(['user', 'comments.user'])
GET /api/posts/1?rel%5B%5D=user&rel%5B%5D=comments.user
returns Post with User + Comments.User
but anyone can easily mess with the 'rel' parameter so I also need to check that
¯\(°_o)/¯
3. Just create a new endpoint for every specific requirements. (what should your endpoint look like for the example above?).
I'm building a SPA with Angular + Laravel (just a self-consumed API) for my Internal Project when I encounter this pitfall. The second approach is what I currently using for basic fetching and I use the third approach for more complex requirements.
Any inputs are appreciated.
I'm following Album tutorial on Zend Framework 2's website, and I want to send object that have been converted to Json and pass controller via AJAX, I can invoke the method on the controller as well as receive Json from controller, but I don't know how to send parameter to it.
Thanks for reading and sorry for my bad English.
Do you mean pushing parameters to your controller via AJAX, like this?
$.post('/your/url', { value: val, row: row, element: element }, //parameters
function (ret) {
});
I'm creating an API using Cakephp 2.x that needs a POST request to post some data to the server however when I'm posting (using Postman) to 127.0.0.1/appname/api/confirm with code=123 in the post parameters my $_POST is an empty array.
My route works, I can see variables that I declare and output within the controller, and I've checked that the parameters are being passed in the request by using the chrome developer console checking the network data.
Router::connect('/api/confirm', array('controller' => 'awesomeController', 'action' => 'confirm'));
<?php
class AwesomeController extends AppController {
public function confirm() {
$this->autoRender = false;
$this->layout = 'ajax';
pr($_POST);
}
}
?>
I've got my endpoints for the get requests to work just fine, it only seems to be POST data.
Not quite sure why $_POST wouldn't even be available and I'm sure it's something ridiculously silly I've overlooked!
** Edit **
I've attempted the following without success:
$this->request->query
$this->request->data
$this->request->params
I have another method whereby I use GET along with ?parameter=value etc and I am able to use one of the above calls to retrieve the data.
In this case, the variables should be in
$this->request->query
Try using URLs like api/confirm?code=123, and they will be in request->query
I may be wrong since I am pretty new to cakePHP but since you set:
$this->autoRender = false;
so the view is not rendered automatically to set the view to ajax layout.
Isn't it necessary to call:
$this->render();
After setting the layout as said here?
Well, hope it helps.
If anybody come here one day by googling, just had the same problem.
Had a REST Controller, called with URL /rest/something/cool.json
Method called inside RestController.php, had output, but no POST, no REQUEST.
Tried with code=123, sending direct JSON, the only way to make it works was to set Content-Type to application/json and to send actual working JSON : Cake seems to validate prior to anything, sending raw data seems useless.