Laravel雄弁 - で参加し、MAX()およびグループ - それぞれのタイプによって最大を選びます

bysior:

私はまだlaravelと雄弁を学んだし、私は少し問題を抱えています...

私は3つのテーブルを持っています:

Schema::create('fishes', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->bigInteger('user_id')->unsigned();
                $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
                $table->bigInteger('type_id')->unsigned();
                $table->foreign('type_id')->references('id')->on('fish_types')->onDelete('cascade');
                $table->float('length');
});

Schema::create('fish_types', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('name')->unique();
                $table->string('name_raw')->unique();
});

Schema::create('photos', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('photoable_type');
                $table->string('photoable_id');
                $table->string('path');
});

私は、モデルの魚と魚の種類や写真との関係を持っています。そして、その作業は、すべてのものは例えば、罰金です。

$f = Fish::with('photos', 'fishery', 'type')->when($filters['userId'], function ($query) use ($filters) {
    return $query->where('user_id', $filters['userId']);
});

しかし、私は写真(熱心ロード)で、もちろん、ユーザーに属する各タイプの最長魚DBから取得します。

私は、mysqlの質問を(yeee、その悪いのthatsが、雄弁なdidntの作業に参加し、私:()があります。

$sql = "
SELECT id
  FROM fishes f1
  JOIN 
     ( SELECT type_id
            , MAX(`length`) AS pb
          FROM fishes
          where user_id = 6
          GROUP BY type_id
     ) AS f2
    ON f1.type_id = f2.type_id 
   and f2.pb = f1.length 
 where f1.user_id = 6
";

私は魚のIDを持っているので、 - しかし、次は何?同じクエリ "ここ(Column_nameのは、Array)"?

$sth = DB::getPdo()->prepare($sql);
$sth->execute();
$quy = $sth->fetchAll(\PDO::FETCH_COLUMN, 0);
$f = Fish::with('photos', 'fishery', 'type')
          ->where('user_id', 6)
          ->whereIn('id', $quy)->get();

短縮魚モデル:

class Fish extends Model
{
    public function type()
    {
        return $this->belongsTo('App\Models\FishType');
    }

    public function fishery()
    {
        return $this->belongsTo('App\Models\Fishery');
    }

    public function photos()
    {
        return $this->morphMany('App\Models\Photo', 'photoable');
    }

    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

写真のモデルと移行:

 public function up()
    {
        Schema::create('photos', function (Blueprint $table) {

            $table->bigIncrements('id');
            $table->string('photoable_type');
            $table->bigInteger('photoable_id');
            $table->string('path');

        });
    }


class Photo extends Model
{

    public function photoable()
    {
        return $this->morphTo();
    }
}

一般的に多分その仕事は、私はそれが間違っているのですか?より良い方法でそれをどのように行いますか?手伝って頂けますか?:)

T.

アーメド・バット:

あなたは、2つのクエリを実行しているが、これは1つのクエリで行うことができます。これを試して

Fish::with('photos', 'fishery', 'type')
    ->join(DB::raw('( SELECT type_id
            , MAX(`length`) AS pb
          FROM fishes
          where user_id = 6
          GROUP BY type_id
     ) AS f2'),function($join){
    $join->on('fishes.type_id','=','f2.type_id')
         ->on('fishes.length','=','f2.pb');
    })
    ->where('user_id', 6)->get();

の代わりに

$sql = "
SELECT id
  FROM fishes f1
  JOIN **strong text**
     ( SELECT type_id
            , MAX(`length`) AS pb
          FROM fishes
          where user_id = 6
          GROUP BY type_id
     ) AS f2
    ON f1.type_id = f2.type_id 
   and f2.pb = f1.length 
 where f1.user_id = 6
";

$sth = DB::getPdo()->prepare($sql);
$sth->execute();
$quy = $sth->fetchAll(\PDO::FETCH_COLUMN, 0);
$f = Fish::with('photos', 'fishery', 'type')
          ->where('user_id', 6)
          ->whereIn('id', $quy)->get();

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=26640&siteId=1
おすすめ