LARAVEL 5.6 - 错误试图获得财产' nsc'非对象

时间:2018-04-17 04:20:00

标签: laravel laravel-5.6

我想从表Group Class中回显NSC的值,但我收到此错误试图获取属性' nsc'非对象

Table Item Name

Table Group Class

模型商品名称

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ItemName extends Model
{
    protected $table = 'tbl_item_name';
    protected $fillable = [
        'inc', 
        'item_name', 
        'short_name', 
        'definition_eng', 
        'definition_ind'
    ];

    public function GroupClass()
    {
        return $this->belongsTo('App\GroupClass', 'nsc', 'inc');
    }
}

模型组类     

namespace App;

use Illuminate\Database\Eloquent\Model;

class GroupClass extends Model
{
    protected $table = 'tbl_group_class';
    protected $fillable = [
        'inc', 
        'nsc', 
        'description', 
        'main_group'
    ];

    public function ItemName()
    {
        return $this->belongsTo('App\ItemName', 'inc', 'nsc');
    }
}

刀片

<td>{{ $ItemName->GroupClass->nsc }}</td>

请帮助解决这个问题,非常感谢

1 个答案:

答案 0 :(得分:0)

你应该试试这个::

<强>刀片

<td> {{ isset($ItemName->GroupClass) ?  $ItemName->GroupClass->nsc  : '' }} </td>

模型商品名称

public function GroupClass()
{
     return $this->belongsTo('App\GroupClass', 'inc', 'nsc');
}

模型组类

public function ItemName()
{
    return $this->belongsTo('App\ItemName', 'nsc', 'inc');
}