搜索查询问题
Issues with search query
我正在尝试 运行 对我的所有评论进行搜索查询。我遇到了很多问题。我将有两个搜索查询,一个是 运行 对评论 ID 的搜索,另一个是 运行 对通过 user_id FK 连接到评论的用户名的搜索。
目前遇到问题:
Too few arguments to function Illuminate\Support\Collection::get(), 0 passed in
表格:
Comment- id、评论、user_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
paginate
函数会立即为您执行查询,因此您在集合上使用 Collection functions after that. The get
函数需要一个键作为参数,这就是问题所在。
要解决此问题,您可以删除 ->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 语句一样。
我正在尝试 运行 对我的所有评论进行搜索查询。我遇到了很多问题。我将有两个搜索查询,一个是 运行 对评论 ID 的搜索,另一个是 运行 对通过 user_id FK 连接到评论的用户名的搜索。
目前遇到问题:
Too few arguments to function Illuminate\Support\Collection::get(), 0 passed in
表格: Comment- id、评论、user_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
paginate
函数会立即为您执行查询,因此您在集合上使用 Collection functions after that. The get
函数需要一个键作为参数,这就是问题所在。
要解决此问题,您可以删除 ->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 语句一样。