Json Empty Index - php

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'];
}

Related

How to retrieve array of objects sent by Ajax in laravel

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.

Laravel send data via request to another project

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');

how to pass data from one application to another in Laravel?

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.

Laravel post with content type json

I'm working with laravel 5 framework, i want to create a rest API and i'm getting some problems.
I have a method post that receives Json and i can't access that content, always say is [] or null
That's my controller function:
public function Register()
{
$teste = Request::json()->all();
$name=$teste['wtf'];
return $name;
}
That returns me this error "Undefined index: wtf" because the array $teste is null
How can I receive JSON properly?
From the image you showed in the comment, I realize that the data you are sending is as form-data and not JSON. Please select raw option and choose json instead of text from drop down and retry what #Moppo said

Cakephp Web Service issue on "add"

I am trying to setup a simple Restfull api using cakephp.
I followed the documentation from the Cakephp site.
But I am encountering the following issue.
I am using Postman plugin to test the Api calls.
I have a table called 'Categories' and in its controller have an action add().
public function add() {
if ($this->request->is('post')) {
$this->Category->create();
if ($this->Category->save($this->data)) {
$message = 'Saved';
}
else {
$message = 'Error';
}
$this->set(array(
'message' => $message,
'_serialize' => array('message')
));
}
}
and in db, I have Category table with schema (id (int 11, PK, A_I), name(varchar(40)), created (datetime), modified(datetime)).
So from postman I send POST requests to http://myProject/categories.json.
From my understanding when i send key:name and value: test, it must save into the database, which works fine. But I must get error when I replace the "key" with something other than name. i.e for exmaple key: name123 and value: test2, But this data too is getting saved in the db without any error except for the name field. i.e it is saving (id:some value, name:"", created:somevalue, modified:someValue)
I dont understand how. Any help will be greatly appreciated.
You will need to provide more info because what you say doesn't make sense. For example what do your posted data look like? Is there any kind of validation in the model? How do you expect a key/value pair to be stored in only one field (specifically name) in the DB?
What happens now is that you tell Cake to save the supplied data ($this->Category->save($this->data)) although you don't check (via cake's validation rules in the model or any other means) that it contains useful arguments and especially Category.name.
As computers will just do what you tell them to do and not what you imply or have in mind, it goes on and sends the calculated created/modified fields to the DB which in turn saves them with the autoincremented ID. Since name doesn't have a UNIQUE or NOT NULL field condition in the DB it is saved as NULL or empty string.

Categories