1

i have table 'country_visit' like this

City     | Created_at
-------------------------------
Jakarta  | 2018-11-15 06:27:22
Jakarta  | 2018-11-12 05:25:24
Jakarta  | 2018-11-11 05:27:23
Jakarta  | 2018-11-15 07:27:22
Denpasar | 2018-11-15 05:27:22
Denpasar | 2018-11-12 05:27:22
Makassar | 2018-11-15 05:27:22
Makassar | 2018-11-11 05:27:22
Makassar | 2018-11-12 05:27:22

I need result like this

City     | dateis    | total
-------------------------------
Jakarta  | 15 Nov    | 2
Jakarta  | 12 Nov    | 1
Jakarta  | 11 Nov    | 1

I was success when use SQL like this

SELECT city,DATE_FORMAT(created_at,"%d %b") AS dateis, COUNT(city) AS total
FROM country_visit
WHERE city = 'Jakarta'
GROUP BY DATE_FORMAT(created_at, '%Y-%m-%d')

How that query equivalent with Laravel queries ? Btw i still using Laravel 5.4

2
  • is laravel a data entity framework? laravel seems php based which is independent of backend data access and thus you should keep separate from or at least modularize. you have sql that works, create a class that passes that data to the front Commented Dec 1, 2018 at 2:38
  • i was tried using db raw, like this DB::select("SELECT city,DATE_FORMAT(created_at,'%d %b') AS dateis,COUNT(city) AS total FROM country_visit WHERE city='Jakarta' GROUP BY DATE_FORMAT(created_at, '%Y-%m-%d')"); . but still doesnt works Commented Dec 1, 2018 at 2:54

2 Answers 2

2

I solve my self

select("city",
DB::raw("DATE_FORMAT(created_at,'%d %b') AS dateis"),
DB::raw("COUNT(city) AS total"))
->where("city","Jakarta")
->groupby(DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d')"))
->get(); 

the key is in

config/database.php

some setting need to change from

'strict' => true,

to

'strict' => false,
Sign up to request clarification or add additional context in comments.

Comments

0
$table = DB::table('country_visit')
        ->select('city', DB::raw('count(*) as total'))
        ->groupBy('dateis')
        ->get();

3 Comments

looks close but they want date too
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'dateis' in 'group statement' (SQL: select city, count(*) as total from country_visit group by dateis)
@Artron Don't you have a dateis column in your table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.