Trying to get property ‘name’ of non-object in laravel while am getting products from whmcs using api
This is my json data from whmcs api
"products": {
"product": [
{
"pid": "1",
"gid": "1",
"type": "hostingaccount",
"name": "super lite",
"description": "1 website",
"module": "cpanel",
"paytype": "recurring",
"pricing": {
"INR": {
"prefix": "u20b9",
"suffix": "INR",
"msetupfee": "0.00",
"qsetupfee": "0.00",
"ssetupfee": "0.00",
"asetupfee": "0.00",
"bsetupfee": "0.00",
"tsetupfee": "0.00",
"monthly": "-1.00",
"quarterly": "-1.00",
"semiannually": "-1.00",
"annually": "1668.00",
"biennially": "3096.00",
"triennially": "3924.00"
}
This is my controller code
class GetProductController extends Controller
{
public function show(){
$products = Whmcs::GetProducts([
'pid',
'name',
'price',
'description'
]);
return view('main.SME_Hosting',['products'=>$products]);
}
}
This is my view code
@foreach ($products as $product)
{{$product->name}}
@endforeach
This is my route
Route::get('SME_Hosting','GetProductController@show');
3
Answers
You need to just change this
{{$product->name}}
to{{$product['name']}}
.Its because of your $products in an array so in order to access any element from the array you need ‘[]’.
Please check your $products if empty or not like below way. You should try below solution.
I am not sure what’s the response you received from your API. BTW use compact to pass data, it’s much more cleaner.
So, to display your result data use the code below. choose either one based on your data type.
Object Array
normal Array
If you happen to receive json data from API. Convert the data into array.
class
It is always good to use dd($products) to learn what kind of data you receive from API.