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 - how to select and display the information (store name/address etc) registered in advance on the wordpress post page
- php - when input type of html form is datetime-local, initial value specified in value attribute is not displayed at the beginni
- i want to get the value of where specified in the query builder in cakephp3
- php - i want to select a predetermined location in the pull-down data
- python - i want to use if to return the specified keyword in the new column when the values in the two columns of the data fra
- php - if there is an id column other than the primary key, the id cannot be obtained
- php - the controller specified in laravel apiresource cannot be found
- php - how to erase a specified element
- php - a system that supports images of a specified size
- php - list is displayed by select, but insert is not possible
- python - select the specified line in the tree view of tkinter
- php - i want to pass a column name string in nuxtjs vuejs and refer to a variable
- get mysql column data in xampp with php
- php - how to display select boxes in conjunction
- php - the value is not retained only in the select box of the inquiry form
- php - i specified post but it is not sent by post (password reset)
- php - i want to extract the laravel zip file to the specified path
- python - read_excel specified range column cannot be read
- php - about the multiple structure of laravel's many-to-many relations
- php : Notification cannot be handled due to "ERROR: Class'App \ Http \ Controllers \ Api \ Firebase \ Factory' not found" error
- php - when accessing the web application in lan from a pc in another lan, it cannot be referenced how can i make this visible?
- php - error in laradock docker-composeyml
- php - trying to access array offset on value of type null how to avoid
- php - i want to display the image file saved in the storage folder on the page
- php - why is the error statement returned in the url even though i wrote it with try catch?
- i want to get rid of the no route to host error in php file_get_contents ()
- php - laravel: i don't know how to send a variable from the controller
- php - please tell me how to put together the route of laravel