这不是重复的问题,我已经尝试了到目前为止在StackOverflow中找到的大多数解决方案。
我有一个控制器将一个Laravel集合传递给一个视图,如下所示:
$votes = Vote::where('spid', '=', $id)->where('user_id', '=', Auth::user()->id)->get()->keyBy('comment_id');它会生成类似如下的内容:
App\Vote Object
( [attributes:protected] => Array ( [id] => 2 [comment_id] => 13 [user_id] => 1 [vote] => 1 [spid] => 101190045 ) )(还有其他属性,如incrementing或keyType,但它们都被省略了)。
然而,当我在视图中使用这样的东西时,
{{$votes->get($comment->id)->vote}}我得到了:
Trying to get property 'vote' of non-object当我尝试的时候
{{$votes->get($comment->id)['vote']}}我得到了:
Trying to access array offset on value of type null我确信传递的$votes是有效的,因为当我在视图中执行{{($votes->get($comment->id)->vote))}}时,我会得到{"id":2,"comment_id":13,"user_id":1,"vote":1,"spid":101190045}。
奇怪的是,当我还执行dd($votes->get($comment->id)->vote)时,它会返回正确的值。
发布于 2020-05-22 02:06:54
多亏了Christophe Hubert,我意识到它是在一个foreach循环中,并且一些结果返回null。我就是这么做的
{{$votes->get($comment->id)->vote ?? 0}}https://stackoverflow.com/questions/61940482
复制相似问题