问题
I am using Laravel 5.1 in my application.
My MySQL Query is
SELECT * FROM (SELECT uu.*,t.left_id AS meb_id,(CAST(t.pair AS UNSIGNED) - IFNULL(p.pair,0)) AS pair FROM (SELECT left_id,
(CASE
WHEN ((SELECT COUNT(meb_id) FROM user_count WHERE left_id=u.left_id AND active = 1 AND join_date <= '1442255400000') >= (SELECT COUNT(meb_id) FROM user_count WHERE right_id=u.left_id AND active = 1 AND join_date <= '1442255400000')*2)
THEN (SELECT COUNT(meb_id) FROM user_count WHERE right_id=u.left_id AND active = 1 AND join_date <= '1442255400000')
WHEN ((SELECT COUNT(meb_id) FROM user_count WHERE left_id=u.left_id AND active = 1 AND join_date <= '1442255400000') < (SELECT COUNT(meb_id) FROM user_count WHERE right_id=u.left_id AND active = 1 AND join_date <= '1442255400000')*2)
THEN ((SELECT COUNT(meb_id) FROM user_count WHERE right_id=u.left_id AND active = 1 AND join_date <= '1442255400000') /2)
WHEN ((SELECT COUNT(meb_id) FROM user_count WHERE right_id=u.left_id AND active = 1 AND join_date <= '1442255400000') >= (SELECT COUNT(meb_id) FROM user_count WHERE left_id=u.left_id AND active = 1 AND join_date <= '1442255400000')*2)
THEN (SELECT COUNT(meb_id) FROM user_count WHERE left_id=u.left_id AND active = 1 AND join_date <= '1442255400000')
WHEN ((SELECT COUNT(meb_id) FROM user_count WHERE right_id=u.left_id AND active = 1 AND join_date <= '1442255400000') < (SELECT COUNT(meb_id) FROM user_count WHERE left_id=u.left_id AND active = 1 AND join_date <= '1442255400000')*2)
THEN ((SELECT COUNT(meb_id) FROM user_count WHERE left_id=u.left_id AND active = 1 AND join_date <= '1442255400000') / 2) END
) AS pair
FROM user_count AS u WHERE left_id <> 0 GROUP BY left_id) AS t
LEFT JOIN users AS uu ON uu.`id` = t.left_id
LEFT JOIN `total_payment` p ON t.left_id = p.`meb_id`
WHERE t.pair <> 0) AS f WHERE f.pair > 0 AND f.active = 1
How Can I convert it in Laravel Eloquent? I don't know how to use when case
in laravel eloquent.
My Database schema is like this
user_count
table
user
table
total_payment
table
Can anyone help me please?
回答1:
Just surround your raw query with a DB::statement(""), like this :
DB::statement("INSERT INTO `mydatabase`.`mytable` (`id`, `created_at`, `updated_at`, `deleted_at`) VALUES ('7', '2015-09-26 13:38:48', '2015-09-26 13:43:41', NULL)");
It will do the job fine and works with seeds as well.
回答2:
Eloquent is an ORM (Object-relational mapping), it means you can map your database to PHP objects (Eloquent use the Active Record pattern).
It is not a DBAL however you can still execute SQL queries through the DB Facade.
The example provided by you, the user table can be an Eloquent object.
class User extends Model{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'user';
}
Since now you can fetch all user rows like:
$userCollection = (new User)->all(); //Fetch all rows
Or you can find one by PK like:
$user = (new User)->find(123); //123 is the PK of the User
来源:https://stackoverflow.com/questions/32738504/convert-mysql-query-to-laravel-eloquent