I have a table and search bar.
When user input in the search that when I grab that and query my database.
This is what I got
public function getLogsFromDb($q = null) {
if (Input::get('q') != '') {
$q = Input::get('q');
}
$perPage = 25;
if ($q != '') {
$logs = DB::table('fortinet_logs')
->orWhere('account_id', 'like', '%'.$q.'%')
->orWhere('cpe_mac', 'like', '%'.$q.'%')
->orWhere('p_hns_id', 'like', '%'.$q.'%')
->orWhere('g_hns_id', 'like', '%'.$q.'%')
->orWhere('data', 'like', '%'.$q.'%')
->orWhere('created_at', 'like', '%'.$q.'%') <----🐞
->orderBy('updated_at', 'desc')->paginate($perPage)
->setPath('');
//dd($logs);
$logs->appends(['q' => $q]);
} else {
$logs = DB::table('fortinet_logs')
->orderBy('created_at', 'desc')->paginate($perPage)
->setPath('');
}
return view('telenet.views.wizard.logTable', get_defined_vars());
}
Result
In the network tab, I kept getting
Undefined function: 7 ERROR: operator does not exist: timestamp without time zone ~~ unknown
How would one go about debugging this further?
PostgreSQL is strict about different data types. Convert the timestamp column to text:
->orWhereRaw('created_at::text like ?', ['%'.$q.'%'])
sanitize your inputs to avoid such problems
$remove = array("'","!",";","+",'|','||',"&&","}","{","[","]");
$replace = array("","","","","","","","","","","");
$q = str_replace($remove, $replace, $q);
Use the new defined $q in your queris
first step on debuggin to make an sql by tihs. And you look what is the problem for your query.
Like this:
dd($logs = DB::table('fortinet_logs')
->orWhere('account_id', 'like', '%'.$q.'%')
->orWhere('cpe_mac', 'like', '%'.$q.'%')
->orWhere('p_hns_id', 'like', '%'.$q.'%')
->orWhere('g_hns_id', 'like', '%'.$q.'%')
->orWhere('data', 'like', '%'.$q.'%')
->orWhere('created_at', 'like', '%'.$q.'%')//This is the cri
->orderBy('updated_at', 'desc')->toSql());
This is show for you whats the problem of this query.
By the way, can you cast $q to daterange and can find in this range the logs.
Related
i want to filter with like in laravel project and paginate with laravel paginate, i try this code
$search = $request->get('query');
$products= DB::table('products')
->where('nama_produk', 'LIKE', '%'.$search.'%')
->paginate(6);
when use this code nothing error but it return all of data in my database table and not filtered.
if i use this code
$search = $request->get('query');
$products= DB::table('products')
->where('nama_produk', 'LIKE', '%test%')
->paginate(6);
it Worked :(
can someone help me to solve this problem ?
This is my search controller :
public function search(Request $request){
$query = $request->query;
$search = Post::where('post_title', 'LIKE', "%$query%")->orWhere('post_body', 'LIKE', "%$query%")->orWhere('post_tags', 'LIKE', "%$query%")->orderBy('created_at')->paginate(10);
return view('front.search', ['posts'=> $search, 'query'=> $query]);
}
Hope this helps.
I use this : https://github.com/rinvex/repository
My query is like this :
$query = $this->store_repository
->join('favorites', function ($join) {
$join->on('stores.id', '=', 'favorites.favoritable_id')
->where('favorites.favoritable_type', 'like', 'App\\\Models\\\Store');
})
->where('stores.status', '=', 1)
->select('stores.id', 'stores.name', 'stores.photo','stores.address');
if($location)
$query->where('stores.address', 'like', "%$location%");
if($q) {
$query->where('stores.name', 'like', "%$q%")
->where('stores.address', 'like', "%$q%", 'or');
}
$result = $query->orderBy('favorites.updated_at', 'desc')->paginate($num);
When executed, there exist error like this :
Missing argument 3 for
Rinvex\Repository\Repositories\BaseRepository::join()
How can I solve it?
I don't think you need the clojure to make what you want.
I guess this should be fine :
$query = $this->store_repository
->join('favorites', '=', 'favorites.favoritable_id')
->where('favorites.favoritable_type', 'like', 'App\\\Models\\\Store');
->where('stores.status', '=', 1)
->select('stores.id', 'stores.name', 'stores.photo','stores.address');
I want do following query in laravel elequent
select from table where cond1 and(cond or cond or cond..)and cond3.
I have used following code,
$invoice = Invoice::with("items", "salesPerson", "client");
if ($q = Request::input("q")) {
$invoice->where("invoices.invoice_number", 'LIKE', "%$q%");
$invoice->orWhere("invoices.reference_number", 'LIKE', "%$q%");
$invoice->orWhere("invoices.client_name", 'LIKE', "%$q%");
$invoice->orWhere("invoices.invoice_date", 'LIKE', "$q%");
$invoice->orWhere("invoices.due_date", 'LIKE', "$q%");
}
$invoice->whereNull("invoices.deleted_at");
but it return deleted records as well.How can I fix this?
In this case a nested where comes in handy:
$invoice = Invoice::with("items", "salesPerson", "client");
if ($q = Request::input("q")) {
$invoice->where(function($query) use ($q){
$query->where("invoices.invoice_number", 'LIKE', "%$q%");
$query->orWhere("invoices.reference_number", 'LIKE', "%$q%");
$query->orWhere("invoices.client_name", 'LIKE', "%$q%");
$query->orWhere("invoices.invoice_date", 'LIKE', "$q%");
$query->orWhere("invoices.due_date", 'LIKE', "$q%");
});
}
$invoice->whereNull("invoices.deleted_at");
I have built a multi filter search for my website but I cant seem to find any documentation on multiple if statements inside of where for my search.
Returns Lots of Results
$data = Scrapper::paginate(15);
Returns none.. need it to be this way to have where statements with IF see bellow.
$database = new Scrapper;
$database->get();
Example of what I want to do..
$database = new Scrapper;
if (isset($_GET['cat-id'])) {
$database->where('cat_id', '=', $_GET['cat-id']);
}
if (isset($_GET['band'])) {
$database->where('price', 'BETWEEN', $high, 'AND', $low);
}
if (isset($_GET['search'])) {
$database->where('title', 'LIKE', '%'.$search.'%');
}
$database->get();
Very similar to this: Method Chaining based on condition
You are not storing each query chains.
$query = Scrapper::query();
if (Input::has('cat-id')) {
$query = $query->where('cat_id', '=', Input::get('cat-id'));
}
if (Input::has('band')) {
$query = $query->whereBetween('price', [$high, $low]);
}
if (Input::has('search')) {
$query = $query->where('title', 'LIKE', '%' . Input::get($search) .'%');
}
// Get the results
// After this call, it is now an Eloquent model
$scrapper = $query->get();
var_dump($scrapper);
Old question but new logic :)
You can use Eloquent when() conditional method:
Scrapper::query()
->when(Input::has('cat-id'), function ($query) {
$query->where('cat_id', Input::get('cat-id'));
})
->when(Input::has('band'), function ($query) use ($hight, $low) {
$query->whereBetween('price', [$high, $low]);
})
->when(Input::has('search'), function ($query) {
$query->where('title', 'LIKE', '%' . Input::get('search') .'%');
})
->get();
More information at https://laravel.com/docs/5.5/queries#conditional-clauses
I'm trying to search two optional tables using eloquent:
$users = User::where('ProfileType', '=', 2)
->where(function($query) {
$query->where('BandName', 'LIKE', "%$artist%");
$query->or_where('Genre', 'LIKE', "%$genre%");
})->get();
This works fine for return all results when a user does an empty search, but I am not sure how to adjust this for to search for bandname when that is present and vise versa.
Just to explain what happens on answer below:
Eloquent does a tricky thing here: When you call User::where(...) it returns a Database\ Query object. This is basically the same thing as DB::table('users')->where(...), a chainable object for constructing SQL queries.
So having:
// Instantiates a Query object
$query = User::where('ProfileType', '=', '2');
$query->where(function($query) {
// Adds a clause to the query
if ($artist = Input::get('artist')) {
$query->where_nested('BandName', 'LIKE', "%$artist%", 'OR');
}
// And another
if ($genre = Input::get('genre')) {
$query->where_nested('Genre', 'LIKE', "%$genre%", 'OR');
}
});
// Executes the query and fetches it's results
$users = $query->get();
Building on Vinicius' answer here's what worked:
// Instantiates a Query object
$query = User::where('ProfileType', '=', '2');
// Adds a clause to the query
if ($artist = Input::get('artist')) {
$query->where('BandName', 'LIKE', "%$artist%");
// Temp Usernamesearch
$query->or_where('NickName', 'LIKE', "%$artist%");
}
// Genre - switch function if artist is not empty
if ($genre = Input::get('genre')) {
$func = ($artist) ? 'or_where' : 'where';
$query->$func('Genre', 'LIKE', "%$genre%");
}
// Executes the query and fetches it's results
$users = $query->get();
Turns out that the second optional field must use or_where only if $artist is not set.
Thanks for your help
I think this is what youre after. Your view would have a form to search artist/genre one or the other can be set, or both, or none.
$users = User::where('ProfileType', '=', 2);
if (Input::has('artist')) {
$users = $users->where('BandName', 'LIKE', '%'.Input::get('artist').'%');
}
if (Input::has('genre')) {
$users = $users->where('Genre', 'LIKE', '%'.Input::get('genre').'%');
}
$users = $users->get();
$query = FormEntry::with('form')->where('domain_id', $id);
$query->where(function($query) use ($search, $start, $limit, $order, $dir) {
$query->where('first_name', 'LIKE', "%{$search}%")
->orWhere('last_name', 'LIKE', "%{$search}%")
->orWhere('email', 'LIKE', "%{$search}%")
->offset($start)
->limit($limit)
->orderBy($order, $dir);
});
$entries = $query->get();
$totalFiltered = $query->count();