所以,这里是我的控制器:为什么在Laravel中使用WHERE循环获取数据?
$topics = Topic::where('board_id', $id)->with('user')->get();
$topic = Topic::find($id);
$board = Boards::where('id', $id)->get();
return view('boards.show')->with('topics', $topics)->with('topic', $topic)->with('board', $board);
这里是生成的URL代码:
@foreach($board as $boards)
<a href="/topics/create/{{$boards->id}}">Create New Post</a>
<p>No Posts Found</p>
@endforeach
但是如果我删除foreach循环,它给错误:
Property [id] does not exist on this collection instance.
但是,为什么我必须循环,如果它只从电路板表中获得一行?任何解决方案,不为每个循环运行?
是的,这工作。再次感谢。 –
另外你可以先使用()或采取(1) –