Search in laravel for booking by User id - php

I'm trying to make search for booking by user id but it's give me all booking for all users
I want to know how to retrive all booking by user_id from table booking,
Thanks in advance, Please Note that in offline working very well, but online give me all booking for all users.
<form action="{{url('search',Auth::user()->id)}}" method="get">
<div class="form-group">
<label for="Search">Search :</label>
<input class="form-control" placeholder="Search Here" type="text" name="search">
<button type="submit" class="btn btn-success btn-mini deleteRecord">Search</button>
</div>
</form>
</div>
Route:
Route::get('search/{id}','BookAppointController#search');
Controller:
public function search(Request $request, $id)
{
$search=$request->get('search');
$Allappoints=DB::table('bookappoitments')->where('users_id',$id)
->where('gustname','like', '%' .$search. '%')
->orwhere('gustcompany','like', '%' .$search. '%')
->orwhere('gustemail','like', '%' .$search. '%')
->join('times','bookappoitments.times_id','times.id')
->join('dates','bookappoitments.Dates_id','dates.id')
->select('bookappoitments.*','times.from_to','dates.Dates')->orderBy('times.id')
->paginate(10);
return view('admin.ManageTime.All',compact('Allappoints'));
}

Use this format, please
public function search(Request $request, $id)
{
$search=$request->get('search');
$Allappoints=DB::table('bookappoitments')->where('users_id',$id)-
>paginate(10);
return view('admin.ManageTime.All',compact('Allappoints'));
}
Tell me if any other issue.

Related

Search Functionality in Laravel not working

I am trying to search through my posts but clearly something is wrong since i am getting no results whether the query contains data present in my posts or not.
here is my blade file and search function
<h6 class="sidebar-title">Search</h6>
<form class="input-group" action="{{route('welcome-page')}}" method="GET">
<input type="search" class="form-control" name="search" placeholder="Search">
<div class="input-group-addon">
<span class="input-group-text"><i class="ti-search"></i></span>
</div>
</form>
search function
class WelcomeController extends Controller
{
public function index(){
$search = request()->query('search');
if ($search){
$posts = Post::where('title', 'LIKE', '%' .$search. '%')->paginate(1);
}else
{
$posts = Post::paginate(2);
}
return view('welcome')->with('categories', Category::all())
->with('tags', Tag::all())
->with('posts', Post::paginate(2));
}
I was trying to search the get the correct results

How do I show the name of the related tables in my "empleados" datatable after doing a search in Laravel 8?

Sorry for my grammar, english is not my native language.
My data table is working fine when is not doing any search. But, when I do a search with the field nombre is giving me this error:
Undefined property: stdClass::$departamento (View: /Users/xxx/Sites/contablerd/resources/views/rrhh/empleado/index.blade.php)
This is my "Advanced Search" above the table:
<form action="#" method="GET">
<h3>Busqueda Avanzada</h3><br>
<div class="row">
<div class="row g-2">
<div class="col-md">
<div class="form-floating">
<input type="nombre" class="form-control" id="nombre" name="nombre" placeholder="" value="">
<label for="nombre">Nombre</label>
</div>
</div>
</div>
</div><br>
<input type="submit" value="Search" class="btn btn-secondary">
</form>
This is the part of the code where I am using a foreach. This part work fine when I am not using the "Advanced Search" (rrhh/empleado/index.blade.php):
#foreach($empleados as $empleado)
<tr>
<td>{{$empleado->id}}</td>
<td>{{$empleado->nombre}}</td>
<td>{{$empleado->apellido}}</td>
<td>{{$empleado->departamento->det}}</td>
<td>{{$empleado->cargo->det}}</td>
<td>{{$empleado->nomina->det}}</td>
<td>{{number_format($empleado->sueldo,2)}}</td>
<td>{{number_format($empleado->ars_empleado+$empleado->afp_empleado,2)}}</td>
<td>{{number_format($empleado->sueldo-($empleado->ars_empleado-$empleado->afp_empleado),2)}}</td>
<td>
<a class="btn btn-info" href="/empleado/{{$empleado->id}}/edit">Editar</a>
<button class="btn btn-danger">Borrar</button>
</td>
</tr>
#endforeach
This is my index method where I confirm is there is any request to change the query (EmpleadoController.php):
public function index(Request $request)
{
$empleados = \DB::table('empleados');
if($request->input('nombre')){
$empleados = $empleados->where('nombre', 'LIKE', "%".request('nombre')."%");
$empleados = $empleados->get();
}else{
$empleados = Empleado::all();
}
return view('rrhh.empleado.index',
[
'empleados'=>$empleados
]);
}
So when I do a search is giving me the above error. I got all my relationships in Empleado.php model.
I would like to ask too, is this the correct way to do this?
Thanks in advance and happy rest of the day.
Added eager loading for departamento, cargo and nomina relations and used the query based on the model, using DB will not access your relations.
public function index(Request $request)
{
if($request->has('nombre')) {
$empleados = Empleado::with(['departamento', 'cargo', 'nomina'])->where('nombre', 'LIKE', '%' . $request->get('nombre') . '%')->get();
} else {
$empleados = Empleado::all();
}
return view('rrhh.empleado.index', ['empleados' => $empleados]);
}
First, Did you identify the code line where this error was generated Undefined property: stdClass::$departamento ?
Second, it's recommended use compact instead array in the controller, like that
public function index(Request $request)
{
$empleados = \DB::table('empleados');
if($request->input('nombre')){
$empleados = $empleados->where('nombre', 'LIKE', "%".request('nombre')."%");
$empleados = $empleados->get();
}else{
$empleados = Empleado::all();
}
return view('rrhh.empleado.index',compact('empleados'));
}
If there are something wrong in your model, you need share it here to can see how you have the relationships

search keyword in table list

I want to search keyword base on the word on table but when I search there is no output and no error display but the url display the keyword for example http://127.0.0.1:8000/users?keyword=sulaimani
Index.blade.php
<form action="{{ route('users.search') }}" method="GET">
<input type="text" name="keyword" class="form-control" placeholder="Search for...">
<button type="submit" class="btn btn-info">
<i class="fa fa-search"></i> Search
</button>
</form>
UserController.php
public function search(Request $request)
{
$keyword = $request->get('keyword');
$users = User::where('name', 'LIKE', '%' . $keyword . '%')->orwhere('email', 'LIKE', '%' . $keyword . '%')->paginate(4);
return view('users.index')->with(compact('users'));
}
web.php
Route::get('user/search', 'UserController#search')->name('users.search');
First try to find out the name or email like "sulaimani" in your users table. If they are returning what you need then come to the code.
Just dump and die the variable $keyword and see what your are getting (dd($keyword))
Or you can just change the same like:
$keyword = $request->keyword;
Now in dump and die (dd($keyword)) if you are getting your input then just remove dd and hit the following query using eloquent,
$users = User::where('name', 'LIKE', "%{$keyword}%")->OrWhere('email', 'LIKE', "%{$keyword}%")->paginate(4);
Hope this may help you.

Controller - Search System Laravel 5.6

I would like to create a search system on Laravel 5.6.
My real problem is the function "search" to create in my Controller...
I have a table named clubs and I want to get the name_club only.
In my index.blade.php I have this
<form action="/search" method="get">
<div class="form-group">
<input type="text" name="search" class="form-control">
<span class="input-group-prepend">
<button type="submit" class="btn btn-primary">Search</button>
</span>
</div>
</form>
In my ClubController I have this
public function index()
{
$clubs = Club::oldest()->paginate(5);
return view('admin.index', compact('clubs'))
->with('i', (request()->input('page', 1)-1)*5);
}
For my function search() I don't know the syntax... I don't really understand how to integrate my pagination and the "name_club" field in my search function
public function search(Request $request)
{
$search = $request->get('search');
}
Route
Route::resource('/admin', 'ClubController');
Route::resource('/search','ClubController#search');
Thank you for your help.
You can paginate any regular query:
$search = $request->get('search');
$clubs = Club::oldest()
->where('name_club', 'like', "%$search%")
->paginate(5);
For more advanced searches I would recommend using Laravel Scout. Then it becomes something like this:
$search = $request->get('search');
$clubs = Club::search($search)->paginate(5);

How to make a working search form PHP/HTML?

So basically I'm doing a project for college and the only thing missing from my project is a working search form, but i don't know how to do it. All of my products are created and seeded into the database.
My routes:
Route::get('/', [
'uses' => 'ProductsController#getIndex',
'as'=> 'product.index'
] //tenho de meter um . porque nao é uma diretoria global, index ta dentro
duma pasta shop
);
my table:
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string("imagePath");
$table->string("title");
$table->text("description");
$table->integer("price");
});
my form structure:
<form action="" method="post" class="navbar-form navbar-left " role = "search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
my products controller:
public function getIndex(){
$products = Products::all();
return view('shop.index', ['products' => $products]);
}
I've seen a lot of tutorials on how to do one but i cant seem to make it filter the ones i have, for example, if i have 3 boots and 5 shoes on the shop, if i write "boot" on the search, i want only for the 3 boots to appear
your HTML would be
<form action="{{ route('product.search') }}" method="post" class="navbar-form navbar-left " role = "search">
#csrf
<div class="form-group">
<input type="text" class="form-control" name="search" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
in your searchProduct function
for like query where it can search in title and if keyword present it will return all product who has same title you are searching for
public function searchProducts(Request $request){
$products = Product::where('title', 'like', '%'.$request->search.'%')->get();
return view('shop.index', ['products' => $products]);
}
for exact same title result
public function searchProducts(Request $request){
$products = Product::where('title',$request->search)->get();
return view('shop.index', ['products' => $products]);
}
there are a LOT of different ways to approach something like a search ... ranging from simple to very complex but a basic method would to search your title and description fields for a keyword like "boot".
You'll want to add a couple of things ...
First, add a new route to handle the search request:
Route::post('/results', [
'uses' => 'ProductsController#searchProducts',
'as'=>'product.search'
]);
Next, add a name to your html input, such as 'keyword' along with the blade shortcut for the CSRF token field:
<form action="/results" method="post" class="navbar-form navbar-left " role = "search">
#csrf
<div class="form-group">
<input type="text" class="form-control" placeholder="Search" name="keyword">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
Then add a function to your controller to handle the query:
public function searchProducts(Request $request){
$results = Product::where('title', 'like', '%'.$request->search.'%')
->orWhere('description', 'like', '%'.$request->search.'%')
->orderBy('title', 'desc')
->get();
return view('search_results', ['results'=>$results, 'keyword'=>$request->keyword]);
}
Finally, create a new file named search_results.blade.php and place it in your views directory. This will be used to display the search results to the user.
<h2>Search Results for {{$keyword}}</h4>
<table>
<thead>
<tr>
<th>Product</th>
<th>Description</th>
<th>Price</td>
</tr>
</thead>
<tbody>
#forelse($results as $result)
<tr>
<td>{{$result->title}}</td>
<td>{{$result->description}}</td>
<td>{{$result->price}}</td>
</tr>
#empty
<tr>
<td colspan="3">
No Products Found
</td>
</tr>
#endforelse
</tbody>
</table>
With this foundation, you could really go wild and add filtering options, sorting, ranking and more. Hope that helps get you going!
you can do this
html
<form action="/search" method='post' class="navbar-form navbar-left " role = "search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search" name="search">
</div>
<button type="submit" class="btn btn-default">Search</button>
</form>
PHP
public function search(Request $request)
{
$products = Product::where('title', 'LIKE', '%'.request->search.'%')->get();
return view('search', ['products' => $products]);
}
Only change wich view you want return and create too in to a routes/web.php.

Categories