我有一个可以正常工作的原始查询,但我无法将它翻译成 laravel eloquent ......
这是我的桌子:
用户表
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
我的问题是:
- 如果我尝试使用原始查询 via 来执行此操作
DB::raw()
,它会返回一个数组,然后我无法对结果进行分页或排序。 - 我找不到从 Eloquent 的几个表中“选择”的方法
- 我不知道如何从数组中提取数据并获取 Collection
- 我仍然不确定我是否做对了。
那么有没有办法使这项工作?
编辑:
需要明确的是,我想要得到的是:
用户集合,包含:
- 用户 8 的“客户”用户
- 用户 8 的“雇员”用户
- 用户 8。
我可以申请->oldest('seen')->latest()->paginate($n)
或类似的东西。