Using time field in Eloquent model - laravel

I have got a database field containing only a time, it's declared in my migration as
$table->time('time')->default('00:00:00');
Now I want to add a number of seconds to this field using Eloquent, doing something like:
$activetime->time->add(new DateInterval('PT5S'));
(where $activetime is a object of a Eloquent model)
First try on this I got
Call to a member function add() on string
So obviously I have to tell Eloquent that this is not a string but a time. The closest I have got is putting it in protected $dates in the model, but then instead I get
Unexpected data found.
I suspect this is because it isn't really a date but only a time, but I can't seem to find much information about how to handle times. Is there any way of solving this or would I be better off using something else, like dateTime or timestamp (or even an integer for amount of seconds)? What I DO want to record is a users amount of active time for a given date, so for that purpose time seems like the natural choice...

This is because Laravel reads time database type as just a string and you are expecting a DateTime Object.
Add this to your Eloquent model:
public function getTimeAttribute($value)
{
return new \DateTime($value);
}
You can also cast your time to DateTime Object using Laravel Attribute Casting, but you'll have to append some date to your time, because Laravel is expecting correct format

Related

How to query between two datetimes using Laravel and Eloquent but but datetimes are not ordered? (error Prepared statement contains too many place...)

Background:
Data is retrieved every minute from an API and stored in a 'sensors' table.
If the data is older than one year. They will be summarized, saved as one new entry in the database and the old data will be deleted. Therefore the column 'date' is not sorted. An update to a specific date is not possible, since I cannot be sure whether the sensor actually generated data at that time.
Now to my problem:
I have a querie that looks something like this
private function getDataFromPeriod()
{
return Sensor::whereBetween('date', [$this->start, $this->end])->orderBy('date')->get();
}
Where $start and $end are in this format for example: '2022-12-31 01:59:59'.
This function is called during the function render() and that works (at the beginning the data from the last year are sorted)). However, when I call this function repeatedly in a different place, the error 1390 Prepared statement contains too many placeholders comes up.
I suspect this is because the query is not good.
Does anyone have an idea how I can improve the query or where else the error could be?
Thanks for your help
you don't need to sort by date;
just replace your code with this:
return Sensor::where('date','>=', $this->start)->where('date','<=', $this->end)->get();
this line will give you the sensors between your start and end date

Eloquent Compare time

I have a Model which stores the total time spent on page in, the format is HH:MM:SS, i am trying to ask the user to input time in HH:MM:SS and then I would compare the entries value to the one stored in the database.
here is my query so far but its not yeilding any results back.
$assignees = EntryKPITable::where("total_time_inside",'>',$request->number)->whereBetween('created_at',[$from, $to])->get();
I have searched through Eloquent module and only found "WhereDate()" which compare time+date.
all the questions i found did a comparison using DATETIME format(total time can be more than 24h in my case)

Sum of the values of a particular user for a particular month of a particular year

I have a table with time stamp, user id, value. I am trying to get sum of the values of a particular user for a particular month of a particular year. This is what I am using:
$data = Laborhrs::all()->where('employee','7')->sum('value');
This is giving me sum of whole data for employee#7. I am looking to get sum of a particular month of a particular year. Say Sum of values of October 2018. What am I missing?
You forgot to add a condition for the year and month, and you don't need to use all() method.
$sum = Laborhrs::where('employee','7')
->whereMonth('timestamp_column_name', 10)
->whereYear('timestamp_column_name', 2018)
->sum('value');
In addition: I saw your several posts, and I think, you don't understand how laravel eloquent and collections work.
All() method returns collection with all results from database, and then you filters them trough collection (I saw it not first time). It is very slow solution.
Better to use conditions with get() (get() should go after conditions), for example:
$data = Laborhrs::where('employee','7')->get();
It will return collection with results that you need, and you don't need to filter them after getting from database. It will be work much faster.
In that situtation you don't need all() and get() methods at all. Eloquent has sum() method, that will calculate sum in database and return value.

CouchDB View with Multiple Parameters

I have a database that has an id column and a ts column. I need to be able to pass in the id and a start time and end time to retrieve all the values during the specified period. Can I do this with a view, or do I need the view to return all of the values that match the id? My concern is that I will be returning and parsing a lot more data than I really care about. Here is the format of my DB from my current view, which simply returns everything that matches the id...
{"id":"62db2aa3472dce80b1f2193fc21d52fd","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d52fd","_rev":"1-6aadd58f4f5dabacf6f4f638396246d0","id":"A-Meter-KW","ts":1437969600000,"tz":"New_York","val":"191kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d5100","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d5100","_rev":"1-71155153c0f03c49b02850bee5535e22","id":"A-Meter-KW","ts":1437968700000,"tz":"New_York","val":"190kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d45d7","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d45d7","_rev":"1-661511616958d45fdff3307600d2a9ed","id":"A-Meter-KW","ts":1437967800000,"tz":"New_York","val":"189kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d3c23","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d3c23","_rev":"1-4e97cfc6cb97ddc65f04efd9043b3abd","id":"A-Meter-KW","ts":1437966900000,"tz":"New_York","val":"188kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d2e35","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d2e35","_rev":"1-120298e95c9d2b4b9cdf438836b6c0c0","id":"A-Meter-KW","ts":1437966000000,"tz":"New_York","val":"187kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d22b0","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d22b0","_rev":"1-61e55d02bd8f0c601274b904f46c9f34","id":"A-Meter-KW","ts":1437965100000,"tz":"New_York","val":"186kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d1ce2","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d1ce2","_rev":"1-b4fe80563c70a40981e293af9c6a87b3","id":"A-Meter-KW","ts":1437964200000,"tz":"New_York","val":"185kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d1ccc","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d1ccc","_rev":"1-bdf1881c4270e68e7a7ed90a1d945228","id":"A-Meter-KW","ts":1437963300000,"tz":"New_York","val":"184kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d1303","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d1303","_rev":"1-404d5934fc882aa36e6d355d9a3485ae","id":"A-Meter-KW","ts":1437962400000,"tz":"New_York","val":"183kW"}},
{"id":"62db2aa3472dce80b1f2193fc21d0941","key":"A-Meter-KW","value":{"_id":"62db2aa3472dce80b1f2193fc21d0941","_rev":"1-64288d1c98e9b93aa6c546acb1e02078","id":"A-Meter-KW","ts":1437961500000,"tz":"New_York","val":"182kW"}}
...
... my current query is http://localhost:5984/hist/_design/hist/_view/byId?key=%22A-Meter-KW%22&descending=true. I'd like to passing a start and end time as well, something like http://localhost:5984/hist/_design/hist/_view/byId?key=%22A-Meter-KW%22&descending=true&start=1437963300000&end=1437966000000 but cannot figure out how to do this.
EDITED:
In order for Couch to match your query all the data (eg:A-Meter-KW and date) must be in the key, emitted by the view. So I think you might do something like:
emit([key,year,month,day],doc._id)
Then you can use the parameters startkey and endkey to filter the results properly.
Reference: http://guide.couchdb.org/draft/views.html#many
Side consideration: I would not use an "id" property inside my documents, because it could get easily confused with the "_id" (compulsory) one.

Hibernate mapping of two properties to one column

I have an object that is generated from XSDs, so I can't change it. In it I have a String DATE and a String TIME (representing the time of day without the date).
DATE = yyyy-mm-dd
TIME = hh:MM:ss:mmmm
In the OracleDB, I don't want to represent these as VARCHAR. I'd like to use DATE or DATETIME. Therefore, I'd need to map both DATE + TIME to one single column, DATETIME.
This is not possible. You can map two columns to a single property (using composites or user types) but not the other way around.
Using the same column name twice in the mapping file usually results in strange exceptions (index out of bounds).
I would use two columns in the database. Convert them to DATE-kind data types using a user type.

Resources