我需要修改 Eloquent 关系(它可以改变值,或洗牌,或删除某些项目)在控制器中,您可以这样做:$question = Question::first();//$question has a relationship with Answer, therefore I have $question->answers.$question->answers = $this->modifyAnswers($question->answers);dd($question->answers) //Worked! Displaying modified Answersreturn view('someView', compact($question));//In the blade file, it's receiving original $question->answers解决方法是添加具有不同名称的新属性。$question->modifiedAnswers = $this->modifyAnswers($question->answers);//And blade can access $question->modifiedAnswers但是有什么方法可以让我在不添加其他属性的情况下修改值?
1 回答
HUH函数
TA贡献1836条经验 获得超4个赞
你可以,用这个方法 setRelation($name, $value)
$question = Question::first();
$question->setRelation('answers', $this->modifyAnswers($answers));
dd($question->relationLoaded('answers'));
更多信息在这里文档
- 1 回答
- 0 关注
- 133 浏览
添加回答
举报
0/150
提交
取消