I created a search bar, it works well so far. I would like to improve my search bar.
When I enter a name which is not in my listing I will wish to stay on my page index with all the recordings.
For example: If I enter leonard as name
I have a page empty...
Is it possible to stay on my page index with my recordings ?
Here's my relevant code:
public function index(Request $req)
{
if ($req->has('search') && !empty($req->search)) {
$validated = $req->validate([
'search' => 'alpha',
]);
$students = Student::where('name', 'LIKE', '%' . $validated['search'] . '%')->orderBy('name', 'ASC')->paginate(5);
$students->appends($req->only('search'));
return view('admin.students.index', compact('students'));
}
$students = Student::orderBy('name', 'ASC')->paginate(5);
return view('admin.students.index', compact('students'));
}
Related
While making a request to my Laravel database something goes wrong. I'm trying to get different recipes on user input but Postman is returning 'No recipes found'.
My Laravel controller code:
public function search(Request $request) {
$fields = $request->validate([
'search' => 'required|string'
]);
// return response($request);
$recipes = Recipe::where('title', 'LIKE', '%'.$request.'%')->get();
if (count($recipes) > 0 ) {
return response($recipes);
} else {
return response('No recipes found');
}
}
if I return the response it returns "search": "maken" with the request http://127.0.0.1:8000/api/search?search=maken.
When I however hardcode the where clause to $recipes = Recipe::where('title', 'LIKE', 'maken')->get(); it returns some recipes and the search function works. Now I'm not sure whether the error is in my Postman request or in my search function in Laravel. Any help would be greatly appreciated!
You have to use $request->INPUT_NAME or $request->input('name') to get the value, so your code should be:
$recipes = Recipe::where('title', 'LIKE', '%'.$request->search.'%')->get();
Try this way
$recipes = Recipe::where('title', 'like', '%' . request('search') . '%')->get();
if ($recipes->count() > 0 ) {
return response($recipes);
} else {
return response('No recipes found');
}
I'm trying to write a method in my controller to allow searching the results of my query on my view page. The query selects all results from the "ads" table and this method should allow filtering results by the ad's name inputting keywords in the search bar.
The controller code goes like this:
public function index(Request $request)
{
$title = trans('ad.title');
$ads = Ad::paginate(10);
if (!empty($request->input('search_all'))) {
$search_all = urldecode($request->input('search_all'));
$ads->where(function ($query) use ($search_all) {
$query->where('name', 'like', '%'.$search_all.'%')->get();
});
}else {
// Returning to view
return view('admin.ad.list')
->with('ads', $ads)
->with('title', $title);
}
}
However, when I run a search I get the following error: "Missing argument 2 for Illuminate\Support\Collection::where()".
What am I doing wrong?
At the top, when doing $ads = Ad::paginate(10); you have already pulled the results from the database so no more filtering can be done at DB level.
I'd do something like this instead:
public function index(Request $request)
{
$title = trans('ad.title');
$ads = Ad::select();
if ($request->input('search_all')) {
$search_all = urldecode($request->input('search_all'));
$ads->where('name', 'like', '%'.$search_all.'%');
}
// Returning to view
return view('admin.ad.list')
->with('ads', $ads->paginate(10))
->with('title', $title);
}
I have a method index
protected function index(Request $request)
{
$articles = Article::published()->paginate(8);
return view('pages.blog', [
'articles' => $articles,
'orientation' => $this->getOrientation(),
$this->getCategory($request)
]);
}
And a method getCategory()
public function getCategory($request)
{
if ($request->has('category')){
$search = $request->get('category');
$articles = Article::published()
->where('orientation', 'LIKE', '%' . $search . '%')
->paginate(8);
return view('pages.blog', [
'articles' => $articles,
'orientation' => $this->getOrientation()
]);
}
}
As you can see, I try to get my getCategory function outside of my index function.
It works, only, with my debug bar, I have all the SQL queries (those of index and those of getCategory).
Is it possible to optimize my code? Can you help me ?
Thank you
If you need both results add cache for query in index and get full result from cache.
Or if you have all results maybe use collections filter for same your collection from index and provide display data what you need.
For example getCategory add param where you set your collection from index. In article add filter to get only data what you are interest in.
I'm nowhere near an editor to chekc this, but here goes. First function:
public function index(Request $request)
{
$articles = Article::published()->paginate(8);
return view('pages.blog', [
'articles' => $articles,
'orientation' => $this->getOrientation(),
//$this->getCategory($request) - this i don't think can work like this. You are calling
//a function that does not return a value, but instead loads a view, insted:
'categories' => $this->getCategory($request)//the idea here is you call a function and
//get a return as an array
]);
}
Second function:
public function getCategory($request)
{
if ($request->has('category')){
$search = $request->get('category');
$articles = Article::published()
->where('orientation', 'LIKE', '%' . $search . '%')
->paginate(8);
$result = [
'articles' => $articles,
'orientation' => $this->getOrientation()
];
}
else $result = null;
return $result;
}
This might work, once again, im a noob. But i don't see why you need 2 methods for this since there is a if ($request->has('category')){ which will yield a null. Idk, give it a try...
I Have a search box that I want to use to search some of columns of a table in the database. Here's the code
$project = Project::findOrFail($id);
$file = \App\File::find($file);
$query = $request->input('q');
$materials = $query
?\App\Material::where('file_id', '=', $file->id)
->where('material_name', 'LIKE', "%$query%" )->get()
:\App\Material::where('file_id', '=', $file->id)->get();
return view('projects.file',compact('project', 'file', 'materials'));
The data as it is when the page loads is filtered to show just the items from this project. But when the search is done, it searches the whole table. How can I make it search only the items from the specific project and not the whole items from the table?
You can nest your search items this way:
public function handle($request, Closure $next)
{
$project = Project::findOrFail($id);
$file = \App\File::find($file);
$materials = \App\Material::newQuery();
// Did it found the file?
if ($file) {
// Search for the file
$materials->where('file_id', '=', $file->id);
}
// AND, does it have a query to search?
if ($search_item = $request->input('q')) {
// Search for the query
$materials->where('material_name', 'LIKE', "%$search_item%");
}
// AND, does it have a project to filter?
if ($search_item = $request->input('project')) {
// Filter the project
$materials->where('project', 'LIKE', "%$search_item%");
}
// Now go get the results
$materials = $materials->get();
// And return to the view
return view('projects.file',compact('project', 'file', 'materials'));
}
I have a search box when user input roll no, it returns the specific student result. I want to generate PDF of that searched data, that data is in table.
Main Question : I want to generate PDF for the result of student who has searched for his/her data, so how to generate PDF for that current student result.
Print/PDF Generator Button:
Print
PDFController:
class PDFController extends Controller
{
public function getPDF(Request $request){
// I want some code here to get the current student result so that I can generate pdf for the current student
$pdf = PDF::loadView('results.single');
return $pdf->stream('result.pdf');
}
}
SearchController:
class ResultsSearchController extends Controller
{
public function search()
{
$keyword = Input::get('keyword');
$row = Student::where('rollno',$keyword)->first();
$rollno = $row['rollno'];
if($keyword == $rollno){
return View::make('results.single')
->with('search',Student::where('rollno',$keyword)
->get())->with('keyword',$keyword);
}else{
return view('errors.404');
}
}
}
Routes.php:
Route::get('/getPDF', 'PDFController#getPDF');
PS : I am using https://github.com/barryvdh/laravel-dompdf
Try this
First of all change route to
Route::get('/getPDF/{id}', 'yourController#getPDF');
Pass the Searched Student id from single view to PDF view like this
Print
and in your PDF Controller
public function getPDF(Request $request,$id){
$student = Student::findOrFail($id);
$pdf = PDF::loadView('pdf.result',['student' => $student]);
return $pdf->stream('result.pdf', array('Attachment'=>0));
}
and get the object in your view like
{!! $student->property !!}
When you call PDF::loadView() I think you need to include the search results, just as you do in search()
$keyword = Input::get('keyword');
$row = Student::where('rollno',$keyword)->first();
$rollno = $row['rollno'];
if($keyword == $rollno){
$results = Student::where('rollno',$keyword)->get();
$pdf = PDF::loadView('results.single', [
'search' => $results,
'keyword' => $keyword
]);
return $pdf->stream('result.pdf');
}