Laravel 5.8 creates SQL using Eloquent.
SELECT hoge1_column , hoge2_column , hoge3_column , LEAST (COALESCE (MIN (hoge1_flg), 99), COALESCE (MIN (hoge2_flg), 99), COALESCE (MIN (hoge3_flg), 99)) AS hoge_status FROM hoge LEFT JOIN hoge1 ON hoge.fuga = hoge1.fuga LEFT JOIN hoge2 ON hoge.fuga = hoge2.fuga LEFT JOIN hoge3 ON hoge.fuga = hoge3.fuga WHERE hoge_flg = 1 GROUP BY hoge1_column HAVING hoge3_column = 1 OR (hoge2_column = 0 AND hoge_status = 1);
However, I created it with Eloquent as follows in order to put the conditions after having in scope.
self :: selectRaw ('hoge1_column, hoge2_column, hoge3_column') ->selectRaw ('LEAST (COALESCE (MIN (hoge1_flg), 99), COALESCE (MIN (hoge2_flg), 99), COALESCE (MIN (hoge3_flg), 99)) AS hoge_status') ->leftJoin ('hoge1', 'hoge.fuga', '=', 'hoge1.fuga') ->leftJoin ('hoge2', 'hoge.fuga', '=', 'hoge2.fuga') ->leftJoin ('hoge3', 'hoge.fuga', '=', 'hoge2.fuga') ->groupBy ('hoge1_column') ->get ();
To add a condition here
having ('hoge3_column', '=', '1')
Because adding Unknown column came out
where ('hoge3_column', '=', '1')
Changed because I saw it in stackovweflow to use and where.
It went well,
To use hoge_status
havingRaw ('hoge2_column = 0 AND hoge_status = 1')
An Unknown column appears.
The same error appears even if you change to whereRaw.
There are both hoge2 and hoge_status in the Model attribute of Collection acquired without adding the above conditions.
How do I write a where or having condition?
Answer # 1
SELECT hoge1_column , hoge2_column , hoge3_column , LEAST (COALESCE (MIN (hoge1_flg), 99), COALESCE (MIN (hoge2_flg), 99), COALESCE (MIN (hoge3_flg), 99)) AS hoge_status FROM hoge LEFT JOIN hoge1 ON hoge.fuga = hoge1.fuga LEFT JOIN hoge2 ON hoge.fuga = hoge2.fuga LEFT JOIN hoge3 ON hoge.fuga = hoge3.fuga WHERE hoge_flg = 1 GROUP BY hoge1_column
To Eloquent's from
hoge3_column = 1 OR hoge2_column = 0 AND hoge_status = 1
I was able to get the intended result by putting in the where clause.
However, I cannot say anything because it is not a smart method, but for the time being I was able to achieve my purpose.
- php - when reading a text column with select with mysql, characters are cut off in the middle
- php - the a tag does not move according to the specified width wordpress
- php - when input type of html form is datetime-local, initial value specified in value attribute is not displayed at the beginni
- php select statement
- php - i want to create a dynamic select box (laravel, jquery)
- php - [mysql] i want to get the latest 10 rows of data using select
- php - it is a program that displays the select menu using here-documents when i execute it, the display does not appear and it e
- cannot get id column array using select statement in php and mysql
- php - i specified post but it is not sent by post (password reset)
- php - the value is not retained only in the select box of the inquiry form
- php - how to display select boxes in conjunction
- get mysql column data in xampp with php
- php - i want to pass a column name string in nuxtjs vuejs and refer to a variable
- python - select the specified line in the tree view of tkinter
- php - a system that supports images of a specified size
- php - how to erase a specified element
- php - i want to run it on the specified date in laravel's browser test
- php - the controller specified in laravel apiresource cannot be found
- php - if there is an id column other than the primary key, the id cannot be obtained
- php - [laravel] relationed data cannot be displayed in blade
- php - parse error: syntax error, unexpected i want to resolve'$post'(t_variable)
- php - i get the error typeerror: cannot read property'id' of undefined while implementing the message posting function
- php - regarding the construction of laravel environment with docker, the installation was successful and the container was start
- php - the id of the edited data in view cannot be obtained on the confirmation screen
- laravel - i want to get users who meet the condition "i'm following model a&&i'm following model b"
- php - i want to pass the image file to the server
- i run php artisan migrate but can't connect to the database
- php - i want to save the characters edited on the edit screen in the database
- laravel - i want to save $request-> all () and user_id at the same time with eloquent create