What have you done so far? Share some code and we might be able to point you in the right direction.
Just can't make an "Advanced Query" work
Hello everbody! I'm new here in Laracasts... I've been searching for an answer to my question for two or three days, and I can't be able to found it. So I have decided to make a question here in the forum (sorry for my bad, bad english...)
The problem is: I have a SQL Query that works perfectly if executed directly in the DataBase via PHPmyAdmin or any other SQL manager. But I just can´t do the right things to make it work in Laravel.
Here is the query and a short description of what I want:
SELECT *, COUNT(id_profile) AS total_votos FROM (SELECT * FROM likes GROUP BY id_profile) AS sql_likes INNER JOIN lanches ON lanches.id = id_lanche INNER JOIN users ON users.id = lanches.user_id GROUP BY id_lanche ORDER BY total_votos DESC
The app is for a thematic snackbar that serves hamburgers with Starwars and Star Trek themes, and they decided to make a "battle" so the custumers can create their own hamburgers with a custom recipe and etcetera.
The competition now is open for the public to voting and I find out a problem: if someone click repeatedly the "VOTE" button, the system receive multiple queries and now we have a lot of people having thousand of votes. I have blocked by IP, but if the clicks are so fast that the browser doesn't have time to send the request to the new page, the votes are stored anyway. There is a lot of solutions, but what I need here is to make a query that count only one vote per hamburger, and the query do that. So, the guests will see that the error was "revised" and will stop clicking the "VOTE" button.
The system has 3 specific tables to store the information about the user, the hamburger and the votes.
The table "users" stores the Users data. The table "lanches" stores the hamburgers data. The table "likes" stores the votes data.
I need to transport this query to laravel, but I just can't make the things to work properly.
Everytime I have a different error, and now my time is running out.
So... Anyone can help me out? Thank you so much!
Guys, I found out the answer!! Thanks for the replies, but the problem was how the Laravel handle the GroupBy by default, and I changed the values in the database.php file, in the section:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
Changed the 'strict' value to false, and everything seens to be OK!
Please or to participate in this conversation.