Get date from timestamp in laravel - php

How can I get date from "2015-07-31 06:03:21" using Laravel query builder?
In normal query using DATE() we can get date easily, but I don't know how to use DATE() in Laravel query builder. Please check my code sample given below and correct me?
Normal Query
SELECT DATE('2015-07-31 06:03:21'), customer_id FROM customer_histories
Laravel Query Builder
$customerhistory = Customerhistory::where('customer_id', 1)
->select('freetext', 'DATE(created_at)')
->get();

You can do this with DB::raw(), though if you are only selecting it, why not just take created_at complete? By default, Eloquent will convert the created_at and updated_at columns to instances of Carbon so you can handle it like this in your code:
$customerhistory = Customerhistory::where('customer_id', 1)
->select('freetext', 'created_at')
->get();
dd($customerhistory[0]->created_at->toDateString());

You can use DB::raw('something') for that. In this case your raw input will be treated as part of actual SQL.
This is something you might wanna give a try:
$customerhistory = Customerhistory::where('customer_id', 1)
->select('freetext', DB::raw('DATE(`created_at`)'))
->get();
More details and yet another example cal be found here.
Also it looks like you're using eloquent. You might wanna check the mutators section.

$customerhistory = Customerhistory::where('customer_id', '=', '1')
->select('freetext', DB::raw('DATE(`created_at`)'))
->get();
Thanks.

Related

How to convert this MySQL query to Laravel?

Here my MySQL query (work in phpMyAdmin) :
SELECT workcenter, (SUM(w1+w2 +w3 +w4)/ (COUNT(DISTINCT(teknisi))*40*4) )*100 AS total FROM `team` GROUP by workcenter ORDER BY total
then, i try in Laravel Sintax like this below (not work) :
$sql = Team::groupBy('workcenter')->select('workcenter', \DB::raw('(SUM(w1+w2+w3+w4)/ (COUNT(DISTINCT(teknisi))*30*4) )*100 AS total'))
->OrderBy('total', 'Desc')
->get();
When i run the laravel sintax, its doesn't show any errors, but the output is nothing..
Please anyone help me to convert the MySQL query to Laravel Sintax. Thank you!
I think you are close enough, however, this doesn't look like a correct way to group by with Eloquent ORM. Try using raw expressions, something like this might work:
$sql = DB::table('team')
->select(DB::raw('workcenter, (SUM(w1+w2 +w3 +w4)/ (COUNT(DISTINCT(teknisi))*40*4) )*100 as total'))
->orderBy('total', 'desc')
->groupBy('workcenter')
->get();
More about raw expressions here - https://laravel.com/docs/6.x/queries#raw-expressions
Whenever I want to convert SQL query to Laravel I always change one column name, the laravel error report will show your current query and u can compare it to the SQL query

Laravel 5.2 version of whereDate

What is laravel 5.2 version of the below condition:
->whereDate('created_at', '=', $sel_date)
Please note that $sel_date is in the below format :
$sel_date = date('Y-m-d');
Laravel whereDate method generates the following query:
.... WHERE DATE('created_at') = 'Y-m-d date here'
You can get the same query by using a raw where:
->where(\DB::raw("DATE(created_at) = '".$sel_date."'"));
UPDATE
A good approach is to use bindings in raw queries so the right way to write the query would be this:
->where(\DB::raw("DATE(created_at) = '?'", [$sel_date]));
When the variable $sel_date is created from you in code is not a problem using first approach, but when it is a user input can cause a SQL Injection if you do not use bindings ore do not sanitize user input.

Add custom field during select mysql in laravel query

I have this query in laravel 5.2
$obj_custom_stdy_data = QstCustomStudyData::where('student_id', $this->data_user['student_id'])
->select($list_id . ' as list_id ', 'chapter_id', 'subject_id', 'subject_code_id')
->get()
->toArray();
Well I have a fixed value $list_id got from top code. Actually I want to add new field during query selection as list_id. However I got error for such that method.
When I tried in mysql IDE for example:
SELECT (1+2) as total, c.* FROM users
Then the result is no wrong at all.
Is that anyway to write in query builder for laravel instead of raw style?
You can take the use of DB::raw() method of QueryBuilder like this:
->select(DB::raw('(1+2) as total'));
See more about Query Builder's Raw Expressions
Hope this helps!

Comparing two columns in Laravel 5

I am using Laravel
Let's say, I have two date fields in my table. If I want to compare them, i can do whereRaw clause.
$query->whereRaw('date1 > date2')->get()
Is there a way to make a modification to date2 inside this query, so it is actually date2-1day?
Something like:
$query->whereRaw('date1 > (date2-1day)')->get()
You are free to call any SQL code in the "raw" part of your query, so you could do sth like below:
$query->whereRaw('date1 > DATE_SUB(date2, INTERVAL 1 DAY)')->get();
Keep in mind that executing SQL code this way will make your queries work only in databases that support such functions.
Another way would be using whereColumn like
$users = DB::table('users')
->whereColumn('updated_at', '>', 'created_at')
->get();
OR
UserTable::whereRaw('column1 != column2')->get();

Coloumn names in Laravel whereBetween

In Laravel
->whereBetween('usage.created_at', array('subscriptions.created_date', 'subscriptions.end_date'))
It gives query as
`usage`.`created_at` between 'subscriptions.created_date' and 'subscriptions.end_date'
But I am expecting the query to treat the subscriptions.created_date and subscriptions.end_date as column itself instead of string value
`usage`.`created_at` between subscriptions.created_date and subscriptions.end_date
Removed the quotes from subscriptions.created_date and subscriptions.end_date.
From their docs I got this but not suitable for my situation as I want column names instead of 1 and 100
$users = DB::table('users')
->whereBetween('votes', array(1, 100))->get();
How can I achieve this. I am New to Laravel.
Alternatively, you can do this with two whereRaw():
->whereRaw('usage.created_at >= subscriptions.created_date')
->whereRaw('usage.created_at <= subscriptions.end_date');
But if you really want to use whereBetween, I don't know :/
I believe you would have to use DB::raw
->whereBetween('usage.created_at', array(DB::raw('subscriptions.created_date'), DB::raw('subscriptions.end_date')))

Categories