Php 视图上不存在方法orderBy
我犯了一个错误 我的视图中不存在方法orderBy 我的看法是:Php 视图上不存在方法orderBy,php,laravel,laravel-5,laravel-5.2,Php,Laravel,Laravel 5,Laravel 5.2,我犯了一个错误 我的视图中不存在方法orderBy 我的看法是: @foreach($post->comments->orderBy('created_at', 'desc') as $comment) {{ $comment->comments }} @endforeach @stop 这是我的控制器: public function store(Request $request) { $post = new post; $post->tit
@foreach($post->comments->orderBy('created_at', 'desc') as $comment)
{{ $comment->comments }}
@endforeach
@stop
这是我的控制器:
public function store(Request $request)
{
$post = new post;
$post->title = $request->title;
$post->save();
return redirect()->route('rules');
}
public function show($title)
{
$post = post::where('title', $title)->first();
return view('post.show', compact('post'));
}
public function storecomment(request $request)
{
$comment = new comment;
$comment->post_id = Crypt::decrypt(Input::get('post_id'));
$comment->comments = $request->comments;
$comment->save();
return redirect()->route('rules');
}
仍然收到错误。您试图在
集合中而不是在查询中使用orderBy()
要使这项工作与您正在做的完全一样,您必须执行以下操作:
@foreach($post->comments()->orderBy('created_at', 'desc')->get() as $comment)
{{ $comment->comments }}
@endforeach
但是,这种方式并不是最好的方式
为了让代码更清晰,您应该将其添加到comments()
方法中,或者创建另一个在模型内部对查询进行排序的方法。您试图在集合中而不是在查询中使用orderBy()
要使这项工作与您正在做的完全一样,您必须执行以下操作:
@foreach($post->comments()->orderBy('created_at', 'desc')->get() as $comment)
{{ $comment->comments }}
@endforeach
但是,这种方式并不是最好的方式
为了让代码更清晰,您应该将其添加到comments()
方法中,或者创建另一个在模型内部对查询进行排序的方法