Laravel toSql does not exists after flatten() - php

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);

Related

is there a way to use where like query in laravel with variable from GET method?

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.

Eloquent where clause for timestamp in Laravel 5.1

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.

How to solve Missing argument 3 for Rinvex\Repository\Repositories\BaseRepository::join()?

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');

Laravel - Where Clause not working

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().

How use orWhere in laravel elequent properly?

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");

Categories