我正在尝试修改Laravel关系,以针对问题返回相关案例。
我的O(height)
模型具有以下关系条目:
question
但是,尝试使用如下变量从Blade访问casedetail:
/**
* A Question has one Case Detail
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function casedetail( $caseId ) {
return $this->hasOne( 'App\CaseDetail' )->where( 'test_case_id', $caseId );
}
回复给我以下错误:
{{ $Question->casedetail(2) }}
如果我用"htmlspecialchars() expects parameter 1 to be string, object given (View: /home/vagrant/Code/TestBench/resources/views/testcases/category_questions.blade.php)"
替换特定数字并从方法中删除变量请求,那么我的查询工作正常:
$caseId
结果:
return $this->hasOne( 'App\CaseDetail' )->where( 'test_case_id', 2 );
我正在尝试从数据库中检索与{"id":1,"test_case_id":2,"question_id":1,"result_type":0,"result":"now what","tester_id":1,"attachment":null,"created_at":"2017-11-30 03:17:42","updated_at":"2017-12-04 06:57:40"}
和CaseDetail
相关的特定question_id
。因此,对于每个问题,请告诉我相关的test_case_id
:
CaseDetail
答案 0 :(得分:0)
我认为一切都是正确的。在这种情况下,您调用关系方法。每个关系方法都返回一个集合的对象。
这里有可能刀片需要单个属性或字符串来打印,方法返回集合?
你试过这样的: {{$ Question-> casedetail(2) - > id}}
尝试打印单个属性希望它有效。