skip to Main Content

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


  1. 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 ‘[]’.

    Login or Signup to reply.
  2. Please check your $products if empty or not like below way. You should try below solution.

    @if(!empty($products)
       @foreach ($products as $product)
          @if(isset($product->name) && $product->name =="")
               {{$product->name}}
           @else
             No record   
          @endif              
    
       @endforeach
    @else
        No record Found....
    @endif
    
    Login or Signup to reply.
  3. I am not sure what’s the response you received from your API. BTW use compact to pass data, it’s much more cleaner.

    class GetProductController extends Controller
    {
        public function show(){
    
        $products = Whmcs::GetProducts([
            'pid',
            'name',
            'price',
            'description'
        ]);
    
        return view('main.SME_Hosting',compact('products'));
       }
     }
    

    So, to display your result data use the code below. choose either one based on your data type.

    Object Array

    @foreach ($products as $product)
              {{$product->name}}              
    
    @endforeach
    

    normal Array

    @foreach ($products as $product)
         {{$product['name']}}        
    @endforeach
    

    If you happen to receive json data from API. Convert the data into array.
    class

    GetProductController extends Controller
        {
            public function show(){
    
            $products = Whmcs::GetProducts([
                'pid',
                'name',
                'price',
                'description'
            ]);
    
            $products = json_decode($products, true); // This will make it Object array.
    
            return view('main.SME_Hosting',compact('products'));
           }
         }
    

    It is always good to use dd($products) to learn what kind of data you receive from API.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search