[英]Error Trying to get property of non-object in laravel
I'm trying to pass two variables (post and answer) to the question.blade view as I click the "submit" button while storing body, pros, cons in the DB. 当我在数据库中存储正文,优点和缺点时,单击“提交”按钮时,我试图将两个变量(发布和答案)传递给question.blade视图。 I receive the following error : 我收到以下错误:
Trying to get property of non-object (View: D:\\wamp\\www\\xxxxxxxxxx\\resources\\views\\Question.blade.php) in 9e68b64a739caa46ec7f5af26c6c40a6cf68ebf3.php line 9 at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44 尝试在9e68b64a739caa46ec7f5af26c6c40a6cf68ebf3.php第9行的CompilerEngine-> handleViewException(object(ErrorException),'1' )在PhpEngine.php第44行
The controller code : 控制器代码:
public function postAnswer(Request $request, $post_id)
{
$post = Post::where('id', $post_id)->first();
$body = $request['body'];
$pros = $request['pros'];
$cons = $request['cons'];
$answer = new Answer();
$answer->body = $body;
$answer->pros = $pros;
$answer->cons = $cons;
$request->user()->answers()->save($answer);
$post->answers()->save($answer);
return view('Question', [
'post' => $post ,
'answer' => $answer]);
The question.blade code : issue.blade代码:
@foreach ($answer as $answers)
<p>{{ $answers->body }}</p>
<p>{{ $answers->pros }}</p>
<p>{{ $answers->cons }}</p>
@endforeach
您正在将$answer
作为单个对象发送,但在刀片中将其作为数组使用,或者尝试不使用@foreach在刀片中直接使用它,或者实际上是在尝试发送答案数组,然后发送$request->user()->answers
或$post->answers
而不是传递'answer'=> $ answer,您需要传递'answer' => $request->user()->answers()->latest()->first()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.