response()->json unable to decode in Laravel [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
We are using Laravel version 5.*
We do not want to change this code in our project
response()->json
It will reflect the whole application and It is terrible. So we need a proper solution and I have found the solution in the comments of this questions.
Thanks.
Here is scenario:
$response = response()->json(['code' => 1,
'message' => 'Content-type is ABC',
'data' => ['code'=>1,'message'=>'Content-type is ABC']
]);
echo json_decode($response); // It always print null and nothing.
dd($response); // print Null
return $response; //You will get proper json response in function return.
enter image description here

Try using
$response = response()->json(['code' => 1,
'message' => 'Content-type is ABC',
'data' => ['code'=>1,'message'=>'Content-type is ABC']
]);
$x = $response->getContent();
return $x;
OR
return $response;
output will be
"{"code":1,"message":"Content-type is ABC","data":{"code":1,"message":"Content-type is ABC"}}"

try getContent():
echo $response->getContent();

$response = response()->json(['code' => 1,
'message' => 'Content-type is ABC',
'data' => ['code'=>1,'message'=>'Content-type is ABC']
]);
You can Direct Print
echo $response->getContent()

Related

PHP Variable of Stripe API [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need help with PHP and the Stripe API.
$stripe->accountLinks->create([
'account' => 'acct_1HtKYcRm0s9vfiNs',
'refresh_url' => 'https://example.com/reauth',
'return_url' => 'https://example.com/return',
'type' => 'account_onboarding',
]);
That gives me back an answer like this:
{
"object": "account_link",
"created": 1606772720,
"expires_at": 1606773020,
"url": "https://connect.stripe.com/setup/s/QaVzgu7GNGFP"
}
I need help to know how to display the "url" in a variable in PHP.
I've tried to do this but it doesn't work:
$link = $stripe->accountLinks->create([
'account' => 'acct_1HtKYcRm0s9vfiNs',
'refresh_url' => 'https://example.com/reauth',
'return_url' => 'https://example.com/return',
'type' => 'account_onboarding',
]);
$data = json_decode($link);
$links = $data->url;
If someone knows what I can do it would help me a lot. Thanks :)
EDIT:
The var_dump returns this:
object(Stripe\AccountLink)#127 (4) { ["object"]=> string(12) "account_link" ["created"]=> int(1606856512) ["expires_at"]=> int(1606856812) ["url"]=> string(47) "https://connect.stripe.com/setup/c/vD45DxCl0hZQ" }
But I just need url string
You do not need to json_decode the resulting account link object. All you need to do is access the URL property on the object returned by Stripe. For example:
$link = $stripe->accountLinks->create([
'account' => 'acct_1HtKYcRm0s9vfiNs',
'refresh_url' => 'https://example.com/reauth',
'return_url' => 'https://example.com/return',
'type' => 'account_onboarding',
]);
$url = $link->url;
echo $url;
You can use the $url variable in a template or to perform any other server-side logic. The structure of the returned account link object can be referenced here:
https://stripe.com/docs/api/account_links/object
I've made it work by doing this:
$link = $stripe->accountLinks->create(
[
'account' => 'acct_1HtKYcRm0s9vfiNs',
'refresh_url' => 'https://example.com/reauth',
'return_url' => 'https://example.com/return',
'type' => 'account_onboarding',
]
);
$var_url = var_export($link->url, true);
$good_url = str_replace("'", "", $var_url);
Now when I echo "good_url" I get the full url successfully.
Thank you all very much, really, you have helped me a lot.

how to handle null values using ternary condition on Serverside datatable script on php? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have to use data table with server side scripting. i have met problem when i have null value in database for particular field. I need to show '-' instead of null value then corresponding value from db if not null.my particular line is
array('db' => 'custname',
'dt' => 2,
'formatter' => function($d,$row) {
return($d == '')?'-': 'db' => 'custname';
})
in fixed condition i have used similar without db values its worked. like,
array('db' => 'status',
'dt' => 4,
'formatter' => function($d,$row) {
return($d == 'a')?'Active':'Hold';
})
But when i need db value if data not null i have struggled.Thanks in advance
Try this
array('db' => 'custname',
'dt' => 2,
'formatter' => function($d,$row) {
return(!isset($d))?'-': $d;
})
updated answer because empty would return true for more cases than only null

Laravel 5.4 sortBy() not working when returned as JSON [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Laravel Doc
My laravel Version
const VERSION = '5.4.15';
HomeController
$collection = collect([
['name' => 'Desk', 'price' => 200],
['name' => 'Chair', 'price' => 100],
['name' => 'Bookcase', 'price' => 150],
]);
$sorted = $collection->sortBy('price');
$sorted->values()->all();
return $sorted;
Result when returned as JSON:
{
0: {
name: "Desk",
price: 200
},
1: {
name: "Chair",
price: 100
},
2: {
name: "Bookcase",
price: 150
}
}
What did I miss? Why isn't sortBy working?

Laravel 5.1 Validation not ignoring unique on update [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Hi I'm having trouble getting laravel to ignore an unique identifier rule when updating. I've tried using $member->id, $id ,'.$member->id ,'.$id etc
public function rules()
{
return [
'first' => 'required|min:2',
'last' => 'required|min:2',
'mobile' => ['required','regex:^(\+614)+([0-9 {8})$^','unique:members,mobile,$id'],
];
}
To use the $id variable in the string, double quote it:
['required','regex:^(\+614)+([0-9 {8})$^',"unique:members,mobile,$id"]
// ^ ^
As long as you are inside a Form Request Validation, you should be able to do this:
['required','regex:^(\+614)+([0-9 {8})$^','unique:members,mobile,'.$this->get(id)]
If you are using Requests for form validation , you can do like this for unique validation :
'unique:table_name ,
unique_column,'.$this->route->getParameter('parameter').',primary_key'
public function rules()
{
return [
'first' => 'required|min:2',
'last' => 'required|min:2',
'mobile' => 'required | regex:^(\+614)+([0-9 {8})$^| unique:members,mobile,'.$this->route()->getParameter('id').',id',
];
}

PHP retrieve variable from file in OOP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a config files which store variable like this:
<?php
return array(
'debug' => true,
'url' => 'http://localhost',
'timezone' => 'UTC',
'locale' => 'en',
);
How can I retrieve it in object oriented way from main file? Thank you.
I'm not quite sure what sort of answer you're expecting when you say "in an object oriented way", but the way I'd do it would be something like the following:
$config = include("configfile.php");
And then you can just access the values like so:
echo $config["timezone"]; //prints "UTC"
i am guessing you wanted to convert this array to an object if so then use this.
<?php
return (object) array(
'debug' => true,
'url' => 'http://localhost',
'timezone' => 'UTC',
'locale' => 'en',
);
in the other page do this:
$config = include("configfile.php");
echo $config->locale;
This is not really a OOP concept, rather a PHP concept
Simply include the file:
$config = include 'config.php';

Categories