无法从视图访问laravel Query Builder结果

时间:2017-07-20 15:24:06

标签: php laravel-5

我无法从控制器访问此变量$ post中的值:

刀片视图中的

@foreach($post as $p)
          <div class="col-md-3">
                 <div class="boxpromo">
                    <h4>{{ $p->title }}</h4>
                    <p>{{ $p->post_description }}</p>
                    <div class="price">
                        <div class="data">{{ $p->out_date }}</div>
                   <div class="cost">{{ $p->price }}</div>
                 </div>
               </div>
           </div>
  @endforeach

在控制器中我有:

public function postSearchTransport(Request $request, Post $post){
...
// create a new query builder
        $post = $post->newQuery();

        // add some filters from the user in the search query
        if($request->has('searchWord')){
            $post->where('title', 'LIKE', '%'.$request->input('searchWord').'%');
        }
...//more filters
$post->paginate(5);
return view('searchVehicle.showSearchPost')->with('post', $post);
}

我做错了什么?我应该能够在视图中访问查询结果中的所有值,但我可以这样做。

我应该将$ post std对象从控制器转换为数组然后将其返回到视图???

1 个答案:

答案 0 :(得分:1)

您必须将paginate方法的返回值传递给视图,如下所示:

$posts = $post->paginate(5);
return view('searchVehicle.showSearchPost')->with('post', $posts);