Laravel - foreach循环返回尝试获取非对象的属性

时间:2017-03-27 18:21:45

标签: php laravel foreach laravel-5.4

我有一个数组集合(18),ALL都有关系 - question

获取数据的功能:

 $auditResults = Audit::where('audit_id', $id)
     ->with('question', 'question.auditQuestion')
     ->get();

在问题数组中,我可以看到我想要访问的属性。当我尝试访问它时,我得到Trying to get property of non-object

收藏:

Collection {#407 ▼
  #items: array:18 [▼
    0 => Audit {#411 ▶}
    1 => Audit {#412 ▶}
    2 => Audit {#413 ▶}
    ... shortened for brevity's sake
    14 => Audit {#425 ▶}
    15 => Audit {#426 ▶}
    16 => Audit {#427 ▶}
    17 => Audit {#428 ▶}
  ]
}

输出Audit

@foreach($auditResults as $answer)
    {{$answer}}
@endforeach
{  
       "id":1,
       "audit_id":1,
       "audit_questions_details_id":2,
       "question":{  
          "id":2,
          "audit_question_id":2,
          "question_number":1,
          "comment":1,
          "header":0,
          "created_at":"2017-03-27 12:16:50",
          "updated_at":"2017-03-27 12:16:50",
          "audit_question":{  
             "id":2,
             "audit_detail_id":1,
             "question":"Lorum Ipsum Dorum",
             "created_at":"2017-03-27 12:16:50",
             "updated_at":"2017-03-27 12:16:50"
          }
       },
       "score":null,
       "comment":null,
       "created_at":null,
       "updated_at":null
    }

如果我继续访问问题关系:

@foreach($auditResults as $answer)
    {{$answer->question}}
@endforeach

它只输出一个数字(我不确定它实际是什么!,因为我希望它输出问题关系):

1
2
3
4

为什么这只输出一个数字,应该输出问题数组本身,如上所示?

当我尝试访问question关系时显示错误:

@foreach($auditResults as $answer)
    {{$answer->question->id}}
@endforeach

审核模式:

class Audit extends Model
{
    protected $fillable = [
        'audit_id', 'audit_questions_details_id', 'question', 'score', 'comment'
    ];

    public function auditScore()
    {
        return $this->belongsTo('App\AuditScore', 'id', 'audit_id');
    }

    public function question(){
        return $this->belongsTo(AuditQuestionDetail::class, 'audit_questions_details_id');
    }

}

审计模型数据:

AuditModelData

AuditQuestionDetail模型:

class AuditQuestionDetail extends Model
{
    protected $table = 'audit_questions_details';

    protected $fillable = [
        'audit_question', 'question_number', 'comment',
    ];

    public function auditQuestion()
    {
        return $this->hasOne(AuditQuestion::class, 'id', 'audit_question_id');
    }

    public function score()
    {
        return $this->hasMany(Audit::class);
    }
}

AuditQuestionDetails Data

非常感谢。

1 个答案:

答案 0 :(得分:1)

我确定,我发现了您的问题:您的表中有question字段用于模型Audit,因此,由于某些原因,Laravel将此字段优先于同名关系question。只需尝试重命名模型中的关系Audit

例如,更改此

public function question(){
    return $this->belongsTo(AuditQuestionDetail::class, 'audit_questions_details_id');
}

就此:

// I know this isn't the best name, just for the test purposes
public function rquestion(){
    return $this->belongsTo(AuditQuestionDetail::class, 'audit_questions_details_id');
}

尝试:

@foreach($auditResults as $answer)
    {{$answer->rquestion}}
@endforeach

如果你在foreach中看到的不只是数字,而是模型AuditQuestionDetail实例的JSON,那么你可以使用这种关系中的任何可用字段:

@foreach($auditResults as $answer)
    {{$answer->rquestion->id}}
@endforeach