If you want a view you can do this
return view('status', compact('result'));
// Or
return view('status')->with(['result' => $result]);
// Or even
return view('status')->withResult($result);
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello...
I using Mailgun API to build a dashboard where subscribers of a newsletter can see their subscription status, following Mailgun's code examples I have:
On my controller I have the following:
public function newsletter(){
$user = User::first();
# Mail gun - Instantiate the client.
$mgClient = new Mailgun(env('MAILGUN_CLIENT'));
$listAddress = env('MAILGUN_LIST_ADDRESS');
# Mail gun - Issue the call to the client.
$result = $mgClient->get("lists/$listAddress/members", array(
'address' => $user->email
));
return response()->json($result, 200, array());
}
When calling the above method I have on my browser the following JSON response, which I believe is great!
{
"http_response_body": {
"items": [
{
"address": "test@testdomain.com",
"name": "Patric Mongan",
"subscribed": false,
"vars": {}
}
],
"total_count": 1
},
"http_response_code": 200
}
I want to pass the above data to a view and echo the name and email address, how do I do that?
I have tried:
public function newsletter(){
$user = User::first();
# Mail gun - Instantiate the client.
$mgClient = new Mailgun(env('MAILGUN_CLIENT'));
$listAddress = env('MAILGUN_LIST_ADDRESS');
# Mail gun - Issue the call to the client.
$result = $mgClient->get("lists/$listAddress/members", array(
'address' => $user->email
));
return response()->json($result, 200, array());
//also
//return view ('status')->with($result);
//also
//var_dump(json_decode(json_encode($result),true));
}
Nothing works, any advice would be much appreciated, please consider I'm just starting with all this Laravel coding... Thank you!.
I think I found the solution... honestly I don't know what is happening there so I cannot explain but you may understand and wish to share...
public function newsletterJsonPhp(){
$user = User::first();
# Mail gun - Instantiate the client.
$mgClient = new Mailgun(env('MAILGUN_CLIENT'));
$listAddress = env('MAILGUN_LIST_ADDRESS');
# Mail gun - Issue the call to the client.
$result = $mgClient->get("lists/$listAddress/members", array(
'address' => $user->email
));
foreach ($result->http_response_body->items as $obj) {
echo $obj->address;
echo $obj->name;
}
Please or to participate in this conversation.