I currently have 2 columns in my table going by the name of uploadDate and uploadTime. I have a clause like this:
->orWhere('uploadDate', 'like', '%' . $request->search . '%')
->orWhere('uploadTime', 'like', '%' . $request->search . '%');
The problem is that if $request->search = yyyy-mm-dd, hh:mm:ss, my web app is not able to get any results since uploadDate and uploadTime are seperate columns on the DB. Does anyone have a way of creating a orWhere statement that concatinates both uploadDate and uploadTime?
I know this can be easily achieved by just creating a single column in the DB that merges both columns but I'm just looking for an easier way out at this point, hahaha.
You shouldn't use like or orWhere() here if you want to find records by exact date and time.
->where('uploadDate', str_before($request->search, ','))
->where('uploadTime', str_after($request->search, ', '))
If you have other where() or orWhere() clauses in the query, you can do this:
->where([
'uploadDate', '=', str_before($request->search, ','),
'uploadTime', '=', str_after($request->search, ', ')
])
Related
I am new to Laravel. I am writing an Eloquent query but it does not work.
the code is
$stories = Stories::with(['user', 'comment'])
->where('blocked', 1)
->where('title', 'like', '%' . $request->title . '%')
->orWhere('story', 'like', '%' . $request->story . '%')
->orWhere('section', 'like', '%' . $request->section . '%')
->orWhere('tags', 'like', '%' . $request->tags . '%')
->orderBy('id', 'DESC')
->get();
but it returns with 'blocked', 0
How can I get the result with 'blocked', 1 as well as the Search value?
You have an order of operations issue. Consider using this version:
$stories = Stories::with(['user', 'comment'])
$query->where('blocked', 1)
->where(function($query) {
$query->where('title', 'like', '%' . $request->title . '%')
->orWhere('story', 'like', '%' . $request->story . '%')
->orWhere('section', 'like', '%' . $request->section . '%')
->orWhere('tags', 'like', '%' . $request->tags . '%')
})
->orderBy('id', 'desc')
->get();
Here is the raw query you currently running:
SELECT *
FROM stories
WHERE (blocked = 1 AND
title LIKE ?) OR
story LIKE ? OR
section LIKE ? OR
tags LIKE ?
ORDER BY id DESC;
Note carefully that I have included parentheses, as the query will actually be evaluted. Here is the version you really want, corresponding to the above Eloquent code:
SELECT *
FROM stories
WHERE blocked = 1 AND
(title LIKE ? OR
story LIKE ? OR
section LIKE ? OR
tags LIKE ?)
ORDER BY id DESC;
Since you need blocked with other fields where you should use advanced query
https://laravel.com/docs/8.x/queries#subquery-where-clauses
$stories = Stories::with(['user', 'comment'])
->where('blocked', 1)
->where(function($q) use ($request) {
$q->where('title', 'like', '%' . $request->title . '%')
->orWhere('story', 'like', '%' . $request->story . '%')
->orWhere('section', 'like', '%' . $request->section . '%')
->orWhere('tags', 'like', '%' . $request->tags . '%');
})->orderBy('id', 'DESC')->get();
I have made this query:
$query = UserHistoryAccess::with('user')
->select('user_history_accesses.*', DB::raw('count(user_history_accesses.id) as total'))
->join('users', 'user_history_accesses.user_id', 'users.id')
->groupBy('user_history_accesses.user_id')
->where('total', 'like', '%' . $term . '%')
;
Even I have followed this solution but I got this error :
Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'total' in 'where clause' (SQL: select count(*) as aggregate from user_history_accesses inner join users on user_history_accesses.user_id = users.id where total like %388%) in file C:\xampp\htdocs\HabilitisBack\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 671
you can not use result colum in the where, you should recalculate it in the where to get the desired result, but try using 'having':
->having('total', 'like', '%' . $term . '%');
I tried this in laravel, but it's not working. I can't get it to work it's working when I use it straight on PHPMyAdmin
SELECT service,
count(*) as total_count,
sum(if(status ='Successful',1,0)) as successful_count,
sum(if(status ='Failed',1,0)) as failed_count
FROM `wallet_ledgers`
WHERE operator = "-"
AND user_id = 5
group by service
this is the desired output The way the table should look
You can use DB::raw
Link: Laravel-raw-expressions
$result = DB::table('wallet_ledgers')
->select(DB::raw("service, count(*) as total_count, sum(if(status ='Successful',1,0)) as successful_count, sum(if(status ='Failed',1,0)) as failed_count"))
->where([
[ 'operator', '=', '-'],
['user_id', '=', '5'],
])
->groupBy('service')
->get();
I have two columns in the users table: first_name & last_name. I want to query the user search john doe. I attempted it like the following.
$users = User::where('username', $request->text)
->orWhere('first_name', 'LIKE', '%'.$request->text.'%')
->paginate(5);
It searches only through john and I don't understand how to make it work
Try the following...
$users = User::where('username', $request->text)
->orWhereRaw("CONCAT(first_name, ' ', last_name)
LIKE ?", ['%' . $request->text . '%'])
->paginate(5);
The whereRaw and orWhereRaw methods can be used to inject a raw where clause into your query.
I have problem with queries in my Controller. I want to count rows and sum by column in one query.
Relations view
So far, I created two queries:
Query to count rows
$paymentsCount = Payments::where('pay_status', 'like', "%{$payStatus}%")->count();
Query to sum
$paymentsValue = Payments::where('pay_status', 'like', "%{$payStatus}%")->sum('brutto');
I have no Idea, how to make on query instead this two queries.
I will be grateful for any help.
The query builder allows you to customize your SELECT clause with the select() method.
Use it to add COUNT() and SUM() in addition of the other columns:
$payments = Payments::select('*', 'COUNT(*) AS count', 'SUM(brutto) AS sum')
->where('pay_status', 'LIKE', "%{$payStatus}%")
->get();