运行paginate

时间:2015-12-27 11:43:57

标签: php laravel laravel-4 closures laravel-pagination

$posts = Post::where('content', 'like', '%'.$keyword.'%')->paginate(5);

当我使用上面的Laravel代码时,页面显示:

  

序列化'关闭'不允许

上面的代码不允许这样写,因为larvel doc似乎没有这个信息。

添加: 当我把它改成

$posts = Post::where('content', 'like', '%'.$keyword.'%')->get();

它显示另一个错误:

  

调用未定义的方法Illuminate \ Database \ Eloquent \ Collection :: links()

我搜索了网页,它告诉我添加paginate(),它可以解决分页链接的问题。但仍然不能

所有代码

   public function search(){
        $keyword = Input::get('keyword');
        if(empty($keyword)){
            return Redirect::route('postIndex');
            //->with('message',array('type' => 'alert', 'content' => '不能为空'))
        }
        $posts = Post::where('content', 'like', '%'.$keyword.'%')->paginate(5);
        return Redirect::route('searchResults')->with('posts', $posts);
   }

   public function searchResults(){
     return View::make('frontend.search.search',['posts' => Session::get('posts')]);
   }

我的路线:

  Route::post('post/search', array(
    'before' => 'csrf',
    'uses' => 'SearchController@search',
    'as' => 'search'  
  ));
  Route::get('post/searchResults', array(

    'uses' => 'SearchController@searchResults',
    'as' => 'searchResults'  
  ));

添加: 发布模型

use Illuminate\Database\Eloquent\SoftDeletingTrait;
class Post extends Eloquent{
  use SoftDeletingTrait;
   protected $dates = ['deleted_at'];

  public static $rules = array(

            'title' => 'required|between:2,80',//unique, 参考users表的设置
            'summary' => 'required|between:20,300',
            'content' => 'required|min:50',

              'tags' => 'required|min:2',
    );  

  protected $fillable = ['title', 'content','summary', 'category'];

  public function tags()
  {
    return $this->belongsToMany('Tag');
  }

    public function category()
  {
    return $this->belongsTo('Category');
  }


  public function user()
  {
    return $this->belongsTo('User');
  }

   public function reviews(){
        return $this->hasMany('Review');//Review is the model of Review
   }

   public function getNumCommentsStr()
   {
    $num = $this->reviews()->count();

    return $num;
   }
}

// $ keyword形式的内容:

{{ Form::open(array('url'=>'post/search','method' => 'post','id'=>'search','class'=>'search'))}}
{{ Form::token()}}
<div class="keywordContainer">
{{Form::text('keyword', '', array('id'=>'keyword', 'placeholder'=>'keyword'))}}
<i class="fa fa-search"></i>
</div>
{{ Form::submit('提交',array('class'=>''))}} 
{{Form::close()}}

1 个答案:

答案 0 :(得分:0)

我认为问题在于:

return Redirect::route('searchResults')->with('posts', $posts);

尝试使用以下方法进行更改:

return Redirect::route('searchResults')->with('posts', $posts->toArray());

并在searchResults更改:

return View::make('frontend.search.search',['posts' => Session::get('posts')]); 

成:

dd(Session::get('posts'));

确保问题出现在第一个方法或重定向后的方法中