1

私は正常に動作する生のクエリを持っていますが、それを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

私の問題は次のとおりです。

  1. を介して生のクエリでそれを実行しようとするとDB::raw()、配列が返され、結果を改ページしたり並べ替えたりすることはできません。
  2. Eloquent で複数のテーブルから「選択」する方法が見つかりません
  3. 配列からデータを抽出してコレクションを取得する方法がわかりません
  4. 私はそれを正しく行っているかどうかはまだわかりません。

それで、それを機能させる方法または他の方法はありますか?


編集:

明確にするために、私が取得しようとしているのは次のとおりです。

以下を含むユーザーコレクション:

  • ユーザー 8 の「クライアント」であるユーザー
  • ユーザー 8 の「従業員」であるユーザー
  • ユーザー 8。

私が申請できる->oldest('seen')->latest()->paginate($n)もの、または同等のもの。

4

1 に答える 1