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

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

不允许序列化“闭包”

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

添加:当我将其更改为

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

它显示另一个错误:

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

我在网上搜索,它告诉我添加分页(),它可以解决分页链接的事情。但仍然不能

所有代码

   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()}}
4

1 回答 1

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'));

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

于 2015-12-27T13:01:28.597 回答