为了对搜索栏进行编程,我创建了一个GET方法,并添加了一个新的控制器,该控制器在其中获取相关数据并将其与视图一起返回。
//This is the form in the view named "index.blade.php"
{!! Form::open(['action' => 'SearchesController@search', 'method' => 'GET']) !!}
<form class="form-inline md-form mr-auto mb-4">
{{Form::text('search', '', ['class'=>'form-control', 'placeholder'=>'Search Anything'])}}
<button class="btn aqua-gradient btn-rounded btn-sm my-0" type="submit">Search</button>
</form>
{!! Form::close() !!}
//---------------------------------------------------------
//This is in the "web.php"
Route::get('/posts/search', 'SearchesController@search');
//---------------------------------------------------------
//The following code is the controller named "SearchesController.php"
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Post;
use DB;
use Illuminate\Support\Facades\Auth;
class SearchesController extends Controller
{
public function search(Request $request)
{
$this->validate($request, [
'search' => 'required',
]);
$search = metaphone($request->input('search'));
$posts = Post::where('sounds_like','LIKE',"%{$search}%")
->orderBy('title', 'desc')
->paginate(10)
->get();
return view('posts.index')->with('posts',$posts);
}
}
我希望该视图的输出包含该视图返回的所有数据,但是却出现了错误消息:
“函数Illuminate \ Support \ Collection :: get()的参数太少,在/Applications/AMPPS/www/lsapp/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php上传递了0第23行,至少应有1个”
答案 0 :(得分:1)
如果您使用的是get(),则不需要paginate()。 Paginate也将执行查询并创建一个集合,并且get()
方法将作为此get()的实例执行,并且需要key参数。因此,您可以将其删除:
public function search(Request $request)
{
// ...
$posts = Post::where('sounds_like','LIKE','%'. $search . '%')
->orderBy('title', 'desc')
->paginate(10);
return view('posts.index')->with('posts',$posts);
}
与错误无关,但是您还有一个form inside a form。选择一个,然后删除另一个:
{!! Form::open(['action' => 'SearchesController@search', 'method' => 'GET', 'class'=>'form-inline md-form mr-auto mb-4']) !!}
{{Form::text('search', '', ['class'=>'form-control', 'placeholder'=>'Search Anything'])}}
<button class="btn aqua-gradient btn-rounded btn-sm my-0" type="submit">Search</button>
{!! Form::close() !!}