[英]Translating Raw sql query to laravel eloquent query
我有一个可以正常工作的原始查询,但是我无法将其翻译为雄辩的laravel ...
这是我的桌子:
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('username', 30)->unique();
$table->string('email')->unique();
$table->string('password', 60);
$table->integer('role_id')->unsigned();
$table->boolean('seen')->default(false);
$table->boolean('valid')->default(false);
$table->boolean('confirmed')->default(false);
$table->string('confirmation_code')->nullable();
$table->timestamps();
$table->rememberToken();
});
Schema::create('clients', function(Blueprint $table)
{
$table->increments('id');
$table->integer('id_marchand')->unsigned()->index();
$table->foreign('id_marchand')->references('id')->on('users')->onDelete('cascade')->onUpdate('restrict');
$table->integer('id_client')->unsigned()->index();
$table->foreign('id_client')->references('id')->on('users')->onDelete('cascade')->onUpdate('restrict');
$table->timestamps();
});
Schema::create('employes', function(Blueprint $table)
{
$table->increments('id');
$table->integer('id_marchand')->unsigned()->index();
$table->foreign('id_marchand')->references('id')->on('users')->onDelete('cascade')->onUpdate('restrict');
$table->integer('id_employe')->unsigned()->index();
$table->foreign('id_employe')->references('id')->on('users')->onDelete('cascade')->onUpdate('restrict');
$table->timestamps();
});
<?php namespace App\Models;
/**
* One to Many relation
*
* @return Illuminate\Database\Eloquent\Relations\hasMany
*/
public function employes()
{
return $this->hasMany('App\Models\Employe', 'id_marchand');
}
/**
* One to Many relation
*
* @return Illuminate\Database\Eloquent\Relations\hasMany
*/
public function clients()
{
return $this->hasMany('App\Models\Client', 'id_marchand');
}
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Client extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'clients';
/**
* One to Many relation
*
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('App\Models\User');
}
}
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Employe extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'employes';
/**
* One to Many relation
*
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('App\Models\User');
}
}
SELECT users.*
FROM clients, users
WHERE clients.id_marchand = 8
AND users.id = clients.id_client
UNION
SELECT users.*
FROM employes, users
WHERE employes.id_marchand = 8
AND users.id = employes.id_employe
UNION
SELECT users.*
FROM users
WHERE users.id = 8
ORDER BY `seen` ASC, `created_at` DESC
LIMIT 25 OFFSET 0
DB::raw()
进行原始查询,它将返回一个数组,然后我无法对结果进行分页或排序。 那么有没有办法使它起作用呢?
明确地说,我想要得到的是:
用户 集合 ,其中包含:
我可以在其上应用->oldest('seen')->latest()->paginate($n)
或类似的东西。
看来您的员工模型设置正确,这应该使它相当简单...
与简单地尝试转换查询以使用查询生成器相比,我认为思考您想做什么以及Eloquent如何帮助您做到这一点更容易。
$id = 8;
$users = App\User::whereHas('clients', function($q) use ($id) {
$q->where('id_marchand', $id);
})->orWhereHas('employes', function($q) use ($id) {
$q->where('id_marchand', $id);
})->orWhere('id', $id)
->orderBy('seen')
->oldest()
->get();
这将返回User
模型的集合。 如果您想分页,只需将get()
换成paginate($numRecords)
,其中$ numRecords是每页所需的记录数。
然后,使用该模型集合,可以使用foreach循环输出每个用户的数据。
foreach ($users as $user) {
echo 'email: ' . $user->email;
}
编辑:我错了,我对模型的关注不够。 因此,在查询中,您要加入客户端,并分别通过id_client
和id_employe
列id_client
表。 所以,如果您修改User
模型,并改变id_marchand
到id_employe
为employes
功能和id_client
为clients
功能,该代码应工作(或至少它确实对我来说)。
只是为了澄清,上面的代码生成以下查询,因此您可以在进行任何更改之前亲自查看结果...
SELECT
*
FROM
`users`
WHERE EXISTS
(SELECT
*
FROM
`clients`
WHERE `clients`.`id_client` = `users`.`id`
AND `id_marchand` = '8')
OR EXISTS
(SELECT
*
FROM
`employes`
WHERE `employes`.`id_employe` = `users`.`id`
AND `id_marchand` = '8')
OR `id` = '8'
ORDER BY `seen` ASC,
`created_at` ASC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.