Я хочу выбрать все единицы измерения, опубликованные всеми пользователями
Таблица user_units (сводная) имеет следующие столбцы:
Код: Выделить всё
id
user_id
unit_id
publish
adtype
addinfo
created_at
updated_at
Код: Выделить всё
public function users() {
return $this->belongsToMany('User', 'user_units')
->withPivot('id','publish', 'adtype', 'addinfo');
}
public function units() {
return $this->belongsToMany('Unit', 'user_units')
->withPivot('id','publish', 'adtype', 'addinfo');
}
Код: Выделить всё
// Get all published units by users for sale.
$users = User::all();
$publishedSaleUnits = [];
foreach($users as $user){
$userUnits = $user->units()->orderBy('adtype', 'desc')->get();
if(count($userUnits)){
foreach($userUnits as $unit){
if($unit->pivot->publish == 1 && $unit->unit_purpose_id == 1){
if( $unit->pivot->adtype ){
//push all featured ads onto the beginning of array
array_unshift($publishedSaleUnits, $unit);
}else{
//push all normal ads onto the end of array
array_push($publishedSaleUnits, $unit);
}
}
}
}
}
Так есть ли лучшее решение, чтобы получить все опубликованные пользователем блоки с разбиением на страницы?
Подробнее здесь: https://stackoverflow.com/questions/270 ... in-laravel
Мобильная версия