Running Laravel 5.6, I found this oddity:
> php artisan tinker
> App\Users::all();
...
created_at: "2018-06-04 16:26:00",
updated_at: "2018-06-04 16:26:00",
....
Pulling in the same element within a Laravel model using:
$users = DB::table('users')->get();
$users->first()->created_at
//2018-04-06 15:59:01
This is the same row. The 35 minute delay is odd, but perhaps due to Homestead latency(?) I have no idea why the month and day are rearranged.
In my middleware, I set the locale information as such:
setlocale(LC_TIME, env('APP_LOCALE_CODE')); //de_DE.UTF-8
date_default_timezone_set(env('APP_LOCALE_TIMEZONE')); //APP_LOCALE_TIMEZONE=Europe/Berlin
Anyone have an idea here?
Turns out the query I needed required multiple joins and groupby sorting so I used DB::table instead of eloquent. When doing so, you need to be careful of the select statement, especially with common DB fields like created_at. Essentially it was pulling in another "created_at" field and by luck, happened to be a reverse of todays current date causing the initial thought it was a formatting error.
Related
Good Evening.
I am trying to get the data from database for Only for last 7 days and show the same in table.
I tried the below code, but its giving me data for 30 days. I am new to coding and a self learner,
Your help will be helpful...
controller code
$sevendays = Carbon::now()->subDays(7);
$dairymilksaleweek = customermilksale::selectraw('(saledate) as "startdate", (SUM(buffalomilk)) as "totalbmilk", (SUM(a2milk)) as "totala2milk", (SUM(jerseymilk)) as "totaljmilk", (SUM(totalmilk)) as "totalmilk"')
->whereDate('saledate', '>=', now()->subDays(7)->startOfDay())
->groupBy('saledate')
->orderBy('saledate')
->get();
Thanks in advance...
As per your query you wanted to get data for last seven days and you are using the whereDate() method of laravel eloquent which needs dates in specified format i.e format('d/m/Y') don't know what's the format for your saledate column but you need to convert second parameter of the function to one of the below.
1- today()->subDays(7)
2- now()->subDays(7)->startOfDay()->format('d/m/Y')
Hope this will help you out.
Used HybridRelations to store user notifications in mongoDB instead of mysql and imported 3million records and now user->notifications()->take(5)->get() or user->notifications()->paginate(5) takes over 30 seconds.
any idea how to speed it up?
on mysql with same amount of records it took 200 milliseconds.
notificaiton model has user_id and description and priority and read flag and user_id is set as an index.
Maybe this suggestion can help you.
When use user->notifications() means the query like that
db.notificaiton.find( {"user_id": <?> } )
That will get all record by user_id.
You can dump the user->notifications() and check. That should be Collection type and contains more than 5 records.
The solution is this.
You can write specific method to ensure what your mongo query expression.
The problem was the column that you are sorting by should be an index as well as the columns you use where clause on.
I am struggling with a pretty difficult thing and hope you can help me out.
Right now I've got the following:
$ads = Ad::where('status', 1)
->whereIn('ad_type', [1, 2, 3])
->where('expire_at', '>', date('Y-m-d H:i:s'))
->where('special_ad', 'standard_ad')
->orderByRaw(DB::raw("FIELD(ad_type,2,3,1)"));
Info:
This is working because it is an Eloquent Collection and I can Paginate this (needed for my Infinite Scroll)
But now I want to shuffle the ad_types in itself, meaning:
ad_type 1 could have, let's say, 30 entries. They will all be returned in the usual order. I want to shuffle those 30 every time I run this query.
I thought about doing ->toArray(); but then again, no pagination (Pagination only works on Eloquent Queries right?)
Then I thought, hey, let's merge this.
But as soon as I did that, the returned collection is no longer an Eloquent Collection but a Support Collection (right? I am not 100% sure it is a Support Collection) thus the Pagination does not work anymore.
I read upon many posts as how to solve this problem, and figured out one solution may be to "create my own paginator instance"
But heck, I am not that good yet. I do really not know, even after studying the laravel documentation, how to create my own paginator.
Important Infos you might need:
Using Laravel 5.2
$ads are dynamical, meaning depending on the case, the requests sent with Ajax, the query might differ at a later point (something might get included).
Thank you very much for your time reading this and hopefully, you can help me and future readers by solving this particular problem.
Greetings, and a great weekend to all of you.
Firstly just to note:
Pagination does not only work for database queries. You can manually paginate using LengthAwarePaginator but manually is the keyword here. The query builder (not Eloquent) can do it automatically for you using paginate.
You can "shuffle" the results by doing something like
$ads = Ad::where('status', 1)
->whereIn('ad_type', [1, 2, 3])
->where('expire_at', '>', date('Y-m-d H:i:s'))
->where('special_ad', 'standard_ad')
->orderByRaw("FIELD(ad_type,2,3,1)")
->orderByRaw("RAND()");
This will order by the ad_type field first and order by a random number (different for every row) as a secondary sort.
Has anyone used DB::whereNotBetween() in Laravel4?
I tried using it for retrieving rows from a table where the field value is not between the given 2 dates, but failed! Here is how I tried it -
DB::table('bookings')->whereNotBetween('arrival',array("2014-01-02","2014-01-10"))->get(array('id'));
This should return all booking rows where the arrival date is not between the above given dates. But instead I am receiving the following error -
"Unknown column 'not_between' in 'where clause' "
not sure what I am doing wrong so all helps are welcomed.
thanks in advance :)
whereNotBetween is a new feature in Laravel 4.1.. Check your version with php artisan -V to be sure. If you don't have 4.1. installed, you can easily follow this guide to upgrade from 4.0.*. I just did it twice last week and it's fairly simple.
If you are on 4.0.*, and can't upgrade for some reason, I would suggest going back to the old tried-and-true: where(...,'>=', $enddate)->where(..., '<=', $startdate).
How do I do the order_by part in codeigniter?
SELECT <field> FROM <table> ORDER BY STR_TO_DATE( <field>, '%H:%i' ) DESC LIMIT 0 , 30
Tried this but it takes "'%H:%i')" as the field name.
$this->db->order_by("STR_TO_DATE(".$field.", '%H:%i')", $order);
The field is a varchar and the query I posted returns the result that I want but I don't know how to do it in codeigniter
I don't think you can do that in CodeIgniter in version 2.1.0. As Madmartigan put it, the comma trigger CI to wrap the wrong part in the backticks and thus giving the mysql error. I think there is no point digging into active record on the order_by function to find a workaround than a real fix for this to work. I check into the upstream and apparently, they have something which is interesting, you might want to keep an eye when is that released.
I didn't spend more time to find workaround in active record order_by but resort to this alternative which seem to work fine. Consider the following?
$sql = "SELECT <field> FROM <table> ORDER BY STR_TO_DATE(?, '%H:%i') desc LIMIT 0, 30";
$this->db->query($sql, array('19:20'));