Laravel Eloquent 3 запроса в один - PullRequest
0 голосов
/ 27 июня 2018

Я пытаюсь выполнить 3 запроса за один раз, чтобы ограничить проблему n1 +:

учитывая, что у нас есть 3 модели:

trips
  id => int
  price => float
  city_id => uint
........

cities
  id => int
  name => varchar
........

ratings:
  id => int
  ratable_id => int
  rate => small-int
......

псевдокод:

select from tours where price >= 100
-then from the result 
select from cities where id in result.city_id as cities
select count from ratings where ratable_id in result.id as rates groupBy rate

так что результат

[
  trips => list of the trips where price more than or equal 100
  cities=> list of the cities those trips belongs to
  rates => list of rating with it's count so like [1 => 5, 2 => 100] assuming that '1 and 2' are the actual rating , and '5,100' is the trips count 
]

как бы мне этого добиться?

Ответы [ 2 ]

0 голосов
/ 27 июня 2018

Модель поездки Отношения

public function city(){
   return $this->belongsTo(City::class);
}

public function ratings(){
   return $this->hasMany(Rating::class, 'ratable_id'); //assuming ratable_id is an id of trips table
}

Выборка данных

$trips= Trip::with('city', 'ratings')
            ->where('price', '>=', 100)
            ->get();

Печать данных

foreach($trips as $trip){
    $trip->city->name." - ". $trip->price." - ". $trip->ratings()->avg('rate');
}
0 голосов
/ 27 июня 2018

Два пути: используйте красноречивые методы, которые предпочтительнее, или объедините один запрос, чтобы получить желаемые результаты

Двигаясь вперед красноречивым образом, я предполагаю, что вы определили свои модели и их отображения на основе их типа отношений (1: m, m: m)

$trips= Trips::with('city')
            ->withCount('ratings')
            ->where('price', '>=', 100)
            ->get();

Движение вперед с присоединением

$trips = DB::table('trips as t')
            ->select('t.id', 't.price','c.name',DB::raw('count(*) as rating_count'))
            ->join('cities as c' ,'t.city_id', '=' , 'c.id')
            ->join('ratings as r' ,'t.ratable_id', '=' , 'r.id')
            ->where('t.price', '>=', 100)
            ->groupBy('t.id', 't.price','c.name')
            ->get();
...