代码之家  ›  专栏  ›  技术社区  ›  detinu20

搜索查询问题

  •  0
  • detinu20  · 技术社区  · 8 年前

    我正在尝试对所有评论运行搜索查询。我遇到了许多问题。我将有两个搜索查询,一个在注释ID上运行搜索,另一个在用户名上运行搜索,该用户名通过用户\id fk连接到注释。

    当前我遇到问题:

    Too few arguments to function Illuminate\Support\Collection::get(), 0 passed in
    

    桌子: 注释-ID、注释、用户ID、时间戳 用户-ID、名称、用户名

    模型:

    class Comment extends Model
    {
    
        public function author()
        {
            return $this->belongsTo('App\User','user_id');
        }
    }
    

    控制器:

    public function index(Request $request)
    {
        $comment =Comment::paginate(10);
    
        $id=$request->input('id');
        $name=$request->input('username');
    
    
        if(!empty($id)){
            $comment->where('id', $request->input('id') )->get();
        }
        if(!empty($name)){
            $comment->where($comment->author->username, 'LIKE', '%'.$name.'%')->get();
        }
    
    
        return view('comments.index')->withComment($comment);
    }
    

    视图:

                <div class="panel-body">
                    {!! Form::open(['route' => 'comments.index', 'method' => 'GET']) !!}
                    <div class="col-md-5">
                        {!! Form::label('id', 'Search By ID:') !!}
                        {!! Form::text('id', null, array('class' => 'form-control')) !!}
                    </div>
                    <div class="col-md-5">
                        {!! Form::label('username', 'Search By Username:') !!}
                        {!! Form::text('username', null, array('class' => 'form-control')) !!}
                    </div>
                    <div class="col-md-2">
                        {!! Form::submit('Find Comments', array('class' => 'btn btn-send ')) !!}
                    </div>
                    {!!Form::close()!!}
    
                </div>
    @foreach($comment as $comments)
    //data
    @endforeach
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   Jerodev    8 年前

    这个 paginate 函数立即为您执行查询,因此您正在使用 Collection functions 之后。这个 get 集合上的函数需要一个键作为参数,这是问题所在。

    要修复此问题,您可以删除 ->get() 或者使用 分页 在查询结束时的函数,如下所示。

    $comment = Comment::query();
    $id = $request->input('id');
    $name = $request->input('username');
    
    if (!empty($id)) {
        $comment = $comment->where('id', $request->input('id'));
    }
    
    $result = $comment->paginate(10);
    
        2
  •  0
  •   Ranjan Adhikari    8 年前

    既然你已经分页了

    public function index(Request $request)
    {
        $comment = new Comment();
    
        $id=$request->input('id');
        $name=$request->input('username');
    
    
        if(!empty($id)){
            $comment->where('id', $request->input('id') )->get();
        }
        if(!empty($name)){
            $comment->where($comment->author->username, 'LIKE', '%'.$name.'%');
        }
    
    
        return view('comments.index')->withComment($comment->paginate(10));
    }
    

    您不能重新分页并获取它,就像在同一对象上使用两次select语句一样。

    推荐文章