Not able to pass complete datetime(Y-m-d H:i:s) in DB::raw in eloquent - eloquent

$shop=DB::table('shops')
->leftJoin('orderbookings',function($join)
{
$join->on('shops.id','=','orderbookings.shop_id');
$join->on('orderbookings.created_at','>=',DB::raw(date("Y-m-d",strtotime("now"))));
})
->select('shops.*')
->selectRaw('COUNT(orderbookings.id) as totalorder, SUM(orderbookings.grand_total) as gtotal')
->orderBy('shops.shop_name', 'asc')
->groupby('shops.id')
->paginate(10);
Above code working fine(But not giving total order and amount correct) and also gives result almost close to what I want,
But I am not able to give date format (Y-m-d H:i:s), it shows syntax error. I am using Laravel 5.2 version
Note: I want to give time as well with date to rectify result,
On giving [example: 2017-03-08 11:15:00] shows syntax error
working query in mysql
SELECT COUNT(orderbookings.id), SUM(orderbookings.grand_total), shops.shop_name FROMshopsLEFT JOIN orderbookings on orderbookings.shop_id = shops.id and orderbookings.created_at BETWEEN "2015-10-22 17:02:02" AND "2017-03-07 17:02:02" GROUP BY shops.id
But not able to to convert in eloquent

You should be able to do the following:
$join->on('orderbookings.created_at', '>=', date("Y-m-d"));
You don't need to use DB::raw, and leaving the second parameter null for date, assumes "now".
If this doesn't solve your issue, please post the exact error you're seeing.

Related

":Mi" has no value in the given object

select to_date(a.game_date|| ' ' ||a.game_time, 'yyyy-MM-dd HH24:Mi') as "beginTime", a.game_id "gameId"
one solution is here https://github.com/sequelize/sequelize/issues/7039, which is a good solution.
I am just wondering if there could be another solution to this issue using some escape syntax like E'foo'?
I tried using E'yyyy-MM-dd HH24:Mi' but didn't work
Edit: Adding more details here
I am calling postgres from nodejs not from the terminal. There the issue is not there. It might be related to how string is parsed here. Changing : to ||CHR(58)|| works perfectly.
to_char(to_timestamp(t/1000), 'YYYY-MM-DD"T"HH24:MI:SS"Z") is throwing the error ":Mi" has no value in the given object

Custom function that returns the latest value

I am trying to create a script using mongo shell in Robo3T, where I can get returned all the latest dates of users that have submitted to userhistories. Although for some reason this script wont work. I am trying to compare 2 rows and then print the date if the ID is the same.
function lastDate(userID) {
print(db.userhistories.find({"user": userID}).sort({date:1}).limit(1));
}
lastDate(db.users.find().forEach(function(val){ val._id.toString()}));
What is wrong with this script? Or is there a better way to fix this issue??

How to prevent the NULLS being removed?

I am currently using SQL Server 2008R2.
I am using this script:
SELECT a.productname, a.orderdate, a.workarea
FROM database1table1 AS a
WHERE a.orderdate >='2016/08/01'
Which gives the output:
PRODUCT NAME ORDER DATE WORKAREA
x 2016/08/07 NULL
y 2016/08/09 HOLDING
z 2016/08/10 ACTION
a 2016/08/12 ACTION
My problem arises when I amend the above script to read,
...
WHERE a.orderdate >='2016/08/01'
**AND a.workarea NOT IN ('HOLDING')**
When I do this, not only does it remove 'HOLDING', but it also removes the NULL rows as well, which I definitely do not want.
Please can you suggest an amendment to the script to prevent the NULLS being removed - I only want to see the value 'HOLDING' taken out.
With many thanks!
You can try a workaround
AND ISNULL(a.workarea,'') NOT IN ('HOLDING')
It will transform all null a.workarea in the "where" the "not in" works correctly

Laravel 5.1 Invalid datetime format: 7 ERROR: invalid input syntax for type date error

I've been having trouble with eloquent that keeps returning error. Below is the query that i tried to run in laravel.
$actives = ProjectVersion::join('version_employees as ve', 've.report_id' ,'=', 'project_versions.id')
->join('employees as e', 've.employee_id', '=', 'e.id')
->whereBetween(\DB::raw("'1985-05-27'::date"),[
\DB::raw("to_date(timeline->>'start_time', 'YYYY-MM-DD')"),
\DB::raw("to_date(timeline->>'release_time', 'YYYY-MM-DD')")])
->groupBy('e.name')
->select(\DB::raw('count(e.id), e.name'))
->get();
Now this returns an error of
Invalid datetime format: 7 ERROR: invalid input syntax for type date: "to_date(timeline->>'start_time', 'YYYY-MM-DD')"
The full query returned by the error message is
SELECT count(e.id), e.name
FROM "project_versions" inner join "version_employees" as "ve" on "ve"."report_id" = "project_versions"."id" inner join "employees" as "e" on "ve"."employee_id" = "e"."id"
WHERE '1985-05-27'::date
BETWEEN to_date(timeline->>'start_time', 'YYYY-MM-DD')
AND to_date(timeline->>'release_time', 'YYYY-MM-DD')
GROUP BY "e"."name"
The thing is that when i run this query in pgadmin, it runs fine and returns the result that i wants.
I've been stuck for hours debugging this. Any idea on where the problem is ?
Well first off, it's not really eloquent in the way you're using it with all those Raw statements ;-)
Try using the Carbon class: Carbon::now()->toDateString() will return a date in the Y-m-d format.
For the where clause, if I'm understanding your query correctly, you can use 2 where clauses instead of the whereBetween with Raw SQL: ->where('start_time', '>', $start_time_preprocessed_by_carbon)->where('release_time', '<', $release_time_preprocessed_by_carbon)
And you seem to want the count which means you can end with a ->count() instead of a ->get()
I'd love to give you the full query but couldn't decipher it completely. However these implementations should give you a nudge in the right direction! They will help you abstract your query further to the point that Laravel's internal query generator will translate the code into the correct syntax for your database!

Running a query using date from a form MS Access

How do I run a query using a value from a textbox from a form I have? I know that there is another post here at Stackoverflow dealing with this issue but I found it to be insufficient for my needs.
I formated my textbox into Medium Date format with its default value being =Date(). However, when I pick up a date and open my report I get this error:
Runtime error 3071: Expression Too Complex
My where clause is this
WHERE
(
(AllInfo.DateOpened >= CDate([Forms]![Main Form]![WindowPrintOptions]![CustomizedReport]!txtDateOpenedFrom.Value))
)
and I am sure it is this code piece that is throwing the problem since when I take it out of the query the error message simply disappears.
Any ideas?
Try with:
(AllInfo.DateOpened >= DateValue([Forms]![Main Form]![WindowPrintOptions].[Form]!txtDateOpenedFrom))
)
Folks,
I got the problem. It was the "AllInfo" alias. It wasn't applicable at that escope inside the query. By changing the proper things, it was enough to write:
[Forms]![Main Form]![WindowPrintOptions]![CustomizedReport]!txtDateOpenedFrom.Value
Issue solved. Thank you all!