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
Laravel 搜索查询的问题_Laravel_Laravel 5_Laravel 5.4 - Fatal编程技术网

Laravel 搜索查询的问题

Laravel 搜索查询的问题,laravel,laravel-5,laravel-5.4,Laravel,Laravel 5,Laravel 5.4,我正在尝试对我的所有评论运行搜索查询。我遇到了一些问题。我希望有两个搜索查询,一个对注释ID进行搜索,另一个对通过用户_ID FK连接到注释的用户名进行搜索 目前我遇到了问题: Too few arguments to function Illuminate\Support\Collection::get(), 0 passed in 表: 注释-id,注释,用户id,时间戳 用户-id、名称、用户名 型号: class Comment extends Model { public

我正在尝试对我的所有评论运行搜索查询。我遇到了一些问题。我希望有两个搜索查询,一个对注释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

{!!Form::open(['route'=>'comments.index','method'=>'GET'])
{!!Form::label('id','Search By id:')
{!!Form::text('id',null,array('class'=>'Form control'))
{!!Form::label('用户名','按用户名搜索:')!!}
{!!Form::text('username',null,array('class'=>'Form control'))
{!!Form::submit('Find Comments',array('class'=>'btn btn send'))
{!!Form::close()!!}
@foreach($comment as$comments)
//资料
@endforeach

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

要解决此问题,您可以删除
->get()
或在查询结束时使用
paginate
函数,如下所示

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

因为你已经分页了

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语句两次一样

我试过了,但没有结果?似乎是Comment::query()没有返回全部的问题?该函数只是为某个模型创建了一个新的查询。您可能需要在if结构之外使用
paginate
函数,我已经更新了答案。