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');
Related
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
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 just worked in the front-end part alone. So I am not sure how to connect or fetch data from a database. If I am using a tomcat server will I be able to do that using PHP?
I am thinking of creating the project in eclipse. I am not sure how to connect that with PHP either.
if you will use Spring as back-end than you need to create model, controller and DAO. Also, you have to create connection to DB. In controller, you have to create method that will call query from DB. After you've created back-end you have to call http.get method inside of js.
e.g.:
$http.get('/user/retrieve/' + $scope.authorization.userName).success(function(data) {
$rootScope.getName = data;
})
.error(function(data){
$rootScope.getName = $scope.authorization.userName;
});
}
After you've created http get method, if call was successful data won't be empty, so you can store it into $scope variable.
To show values of $scope just put double curly brackets around name of $scope like this {{ getName }}
There are plenty of tutorials how to get data from mysql using Spring or php.
I'm giving you some insight by posting link of tutorial: http://websystique.com/springmvc/spring-mvc-4-angularjs-example/
Hope this will help you!
I want to pass data from one application to another in Laravel..
Suppose I want to take user data from one application and I want to send this data to another application form.
I wanted to send it this way..But I'm getting Error..
So if anyone could suggest any solution for this..would be appreciated ..
public function store(Request $request)
{
$payment = new Payment();
$payment->username = $request->Input(['username']);
$payment->price = $request->Input(['price']);
$payment->purchase_id = $request->Input(['purchase_id']);
$payment->save();
$store_id =\Hash::make($payment->id);
$price = $payment->price;
return \Redirect::to('http://localhost/blog/public/getPayment');
}
And I get the following Error:
InvalidArgumentException in Response.php line 462:
The HTTP status code "0" is not valid.
If you just want your user to be redirected to another app so they can fill whatever forms you can use
return Redirect::away('http://localhost/blog/public/getPayment');
See the method here.
BUT
If you want to send data to another app without redirecting the user, the best solution I can give is to make an API on this other app so you can make requests (with Guzzle or something else) to this API, allowing you to pass some data.
I have a ionic application which sends json data to my api which uses laravel as it's backend.
When the post request is made to my route, I call a method on my controller..
This controller then takes the data and enters it into the database, however sometimes not all the data sent from the ionic application maybe there.
I.e. My laravel application is waiting for First Name, Last Name, Company and sometimes my ionic application will only send off first_name, last_name meaning the index for company is missing.
How do I in my controller check if an index exists?
See my json request:
{"data":{"first_name":"Peter","last_name":"tESt"}}
See my controller waiting the data request:
public function store(Request $request)
{
$card = NEW card;
$card->first_name = $request->data['first_name'];
$card->last_name = $request->data['last_name'];
$card->location = $request->data['location'];
$card->save();
};
In some cases the location will be included and in some cases it won't be. The application won't send a empty index either.
In PHP, you can check if a key exists in an array using array_key_exists
In your example, you could do something like:
if (array_key_exists('location', $request->data)) {
$card->location = $request->data['location'];
}