试图在 Laravel 中获取非对象的属性“名称”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49548727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:34:32  来源:igfitidea点击:

Trying to get property 'name' of non-object in laravel

laravel

提问by john

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

尝试在使用 api 从 whmcs 获取产品的同时获取 Laravel 中非对象的属性“名称” 这是我来自 whmcs api 的 json 数据

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

回答by Gabriel

I am not sure what's the response you received from your API. BTW use compact to pass data, it's much more cleaner.

我不确定您从 API 收到的响应是什么。顺便说一句,使用compact 来传递数据,它更干净。

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

如果您碰巧从 API 接收到 json 数据。将数据转换为数组。班级

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.

使用dd($products)来了解您从 API 接收到的数据类型总是很好的。

回答by Parth Pandya

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 '[]'.

您只需将其更改{{$product->name}}{{$product['name']}}. 这是因为您的 $products 在数组中,所以为了访问数组中的任何元素,您需要“[]”。

回答by AddWeb Solution Pvt Ltd

Please check your $products if empty or not like below way. You should try below solution.

请检查您的 $products 是否为空或不喜欢以下方式。您应该尝试以下解决方案。

@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