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");
Related
I am trying to implement a search feature in a site. A user can specify a search string that could match a product_id, product_name or the product_sku. What I want to implement is a search function that would return all that matches.
What I've come with so far:
return Product::with(['getProductItem' => fn ($query) => $query->orWhere('itemSkuNumber', '=', $request->search)])
->where(
'name',
$request->has('match') ? '=' : 'like',
$request->has('match') ? $request->search : '%' . $request->search . '%'
)
->orWhere('id', '=', $request->search)->get();
This has worked if I specify a product_id or product_name but does not return anything if I provide an sku.
You should wrap orWheres with another where because it will try to filter all of them.
Product::with(['getProductItem'])
->whereHas('getProductItem', function ($query) use ($request) {
$query->where(function ($subquery) use ($request) {
$subquery->where('itemSkuNumber', '=', $request->search);
//put your orwhere queries in here
});
})
->get();
Try whereHas
$products = Product::query()
->with(['getProductItem'])
->whereHas('getProductItem', fn ($query) => $query->where('itemSkuNumber', '=', $request->search)]))
->orWhere(
'name',
$request->has('match') ? '=' : 'like',
$request->has('match') ? $request->search : '%' . $request->search . '%'
)
->orWhere('id', '=', $request->search)
->get();
#gguney, #Sumit,
Thanks for your help, :).
Got it working with:
return Product::with(['getProductItem'])
->whereHas('getProductItem', function ($query) use ($request) {
$query->where('itemSkuNumber', '=', $request->search);
})
->orWhere(
'name',
$request->has('match') ? '=' : 'like',
$request->has('match') ? $request->search : '%' . $request->search . '%'
)
->orWhere('id', '=', $request->search)
->get();
#Sumit,
You have extra parentheses and brackets.
Here my code :
$cards = Course::with('cards', 'cards.contexts', 'cards.user_contexts', 'cards.thisUser')
->where('name', 'LIKE', '%'. 'Kanji' .'%')
->get()
->pluck('cards')
->flatten()
->toSql();
dd($cards);
I try to get the SQL code of my eloquent request.
I get this error :
"Method Illuminate\Support\Collection::toSql does not exist."
Anyone have an idea of the problem ? Thanks !
Because after calling get() the query is already executed and a collection is returned with which pluck() continues. You have some other possibilities:
Rewrite your statement to use Illuminate\Database\Eloquent\Builder::pluck() instead of Illuminate\Support\Collection::pluck() but without flatten()
$cards = Course::with('cards', 'cards.contexts', 'cards.user_contexts', 'cards.thisUser')
->where('name', 'LIKE', '%'. 'Kanji' .'%')
->pluck('cards')
->get()
->toSql();
dd($cards);
Call toSql() directly on the query and omit pluck() and flatten()
$cards = Course::with('cards', 'cards.contexts', 'cards.user_contexts', 'cards.thisUser')
->where('name', 'LIKE', '%'. 'Kanji' .'%')
->get()
->toSql();
dd($cards);
Dump something other than SQL → the collection instead
$cards = Course::with('cards', 'cards.contexts', 'cards.user_contexts', 'cards.thisUser')
->where('name', 'LIKE', '%'. 'Kanji' .'%')
->get()
->pluck('cards')
->flatten();
dd($cards);
You can use db enable query log
DB::enableQueryLog();
$data = $order->all();
$cards = Course::with('cards', 'cards.contexts', 'cards.user_contexts', 'cards.thisUser')
->where('name', 'LIKE', '%'. 'Kanji' .'%')
->get()
->pluck('cards')
->flatten();
$query = DB::getQueryLog();
dd($query);
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.
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 am trying to build a search form with laravel, but I cannot get the where clause to work.
$term = $request->input('term');
$count = DB::table('members as m')
->where(DB::raw('m.member_first_name'), 'LIKE', "%$term%")
->orWhere(DB::raw('m.member_last_name'), 'LIKE', "%$term%")
->orWhere(DB::raw('m.member_business_address'), 'LIKE', "%$term%")
->orWhere(DB::raw("concat('m.member_first_name',' ','m.member_last_name')"), 'LIKE', "%$term%")
->orWhere(DB::raw("concat('m.member_last_name',' ','m.member_first_name')"), 'LIKE', "%$term%")
->orWhere(DB::raw("concat('m.member_name_affix',' ','m.member_last_name',' ','m.member_first_name')"), 'LIKE', "%$term%")
->orWhere(DB::raw("concat('m.member_first_name',' ','m.member_name_affix',' ','m.member_last_name')"), 'LIKE', "%$term%")
->count();
var_dump($count);
var_dump($count) always returns all the database entries, no matter what the search term is.
This is my first Laravel project and I would be very thankful for any kind of help.
Perhaps try encasing the query in an overall where:
$count = DB::table('members as m')
->where(function ($q) {
$q->where(DB::raw('m.member_first_name'), 'LIKE', "%$term%")
->orWhere(DB::raw('m.member_last_name'), 'LIKE', "%$term%")
->orWhere(DB::raw('m.member_business_address'), 'LIKE', "%$term%")
->orWhere(DB::raw("concat('m.member_first_name',' ','m.member_last_name')"), 'LIKE', "%$term%")
->orWhere(DB::raw("concat('m.member_last_name',' ','m.member_first_name')"), 'LIKE', "%$term%")
->orWhere(DB::raw("concat('m.member_name_affix',' ','m.member_last_name',' ','m.member_first_name')"), 'LIKE', "%$term%")
->orWhere(DB::raw("concat('m.member_first_name',' ','m.member_name_affix',' ','m.member_last_name')"), 'LIKE', "%$term%")
})
->count();
You're using and / or together, which of course will fail. Try the following:
$count = DB::table('members as m')
->where(DB::raw('m.member_first_name'), 'LIKE', "%$term%")
->where(function ($query) {
$query->where(DB::raw('m.member_last_name'), 'LIKE', "%$term%")
->orWhere(DB::raw('m.member_business_address'), 'LIKE', "%$term%"));
})
Simply try this code and if you got some syntax error then try to change the ' symbols here and there..as I can't test it before post..
$term = "%".$request->input('term')."%";
$count = DB::table('members as m')
->where('m.member_first_name', 'LIKE', $term)
->orWhere('m.member_last_name', 'LIKE', $term)
->orWhere('m.member_business_address', 'LIKE', $term)
->orWhereRaw('(concat_ws(m.member_first_name,m.member_last_name) LIKE ? )',[$term])
->orWhereRaw('(concat_ws(m.member_last_name,m.member_first_name) LIKE ? )',[$term])
->orWhereRaw('(concat_ws(m.member_name_affix,m.member_last_name,m.member_first_name) LIKE ? )',[$term])
->orWhereRaw('(concat_ws(m.member_first_name,m.member_name_affix,m.member_last_name) LIKE ? )',[$term])
->count();
var_dump($count);
you can use whereRaw() don't need to put DB::raw() inside where().