Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 视图上不存在方法orderBy_Php_Laravel_Laravel 5_Laravel 5.2 - Fatal编程技术网

Php 视图上不存在方法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

我犯了一个错误

我的视图中不存在方法orderBy

我的看法是:

@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()
方法中,或者创建另一个在模型内部对查询进行排序的方法