S
Size: a a a
S
S
MP
AK
T
MP
T
MP
MP
T
Н
MP
T
SS
T
П
П
class PostController extends Controller
{
public function postsList($path=null){
Breadcrumbs::push('Блог',URL::route('posts.list'));
$posts = Posts::select('*');
$letters = Posts::selectRaw('UCASE(SUBSTR(`posts`.`title`,1,1)) as `char`');
$filters=[];
View::share('title', 'Блог');
View::share('subtitle', 'Здесь вы найдете что-нибудь интересное');
if($path){
$uri = Str::afterLast($path,'/');
$cat=Categories::where('uri','=',$uri)->with(['ancestors','descendants','ancestors.ancestors'])->firstOrFail();
abort_if($cat->getPath()!=$path,404);
$cat->buildBreadcrumbs();
$cat_ids = clone $cat->descendants;
$cat_ids = $cat_ids->push($cat)->pluck('id')->toArray();
$posts=$posts->whereIn('category_id',$cat_ids);
$letters = $letters->whereIn('category_id',$cat_ids);
$filters['path']=$path;
View::share('title', 'Блог / Категория: '.$cat->name);
View::share('subtitle', $cat->subtitle);
}
//Фильтр по букве названия
if (preg_match('/^[0-9a-zа-я]{1,1}$/iuU', request()->get('char', null))) {
$filters['char'] = Str::upper(request()->get('char', null));
$posts->where(DB::raw('UCASE(SUBSTR(`posts`.`title`,1,1))'),'=',$filters['char']);
$char=Posts::selectRaw('? as `char`',[$filters['char']??'*']);
$letters=$letters->union($char);
}
$page=LengthAwarePaginator::resolveCurrentPage();
$rpp=20;
$cnt= clone $posts;
$cnt=$cnt->count();
$total=ceil($cnt/$rpp);
if( $page>$total){
$page=$total;
}
$offset=( $page-1)*$rpp;
$posts=$posts->with(['user','tags','category','category.ancestors'])->limit($rpp)->offset($offset)->get();
$posts=new LengthAwarePaginator($posts,$cnt,$rpp, $page,['path'=>url()->current()]);
$letters= $letters->distinct()->orderBy('char','asc')->get();
return view('posts.list',compact('posts','letters','filters'));
}
}
Gt
class PostController extends Controller
{
public function postsList($path=null){
Breadcrumbs::push('Блог',URL::route('posts.list'));
$posts = Posts::select('*');
$letters = Posts::selectRaw('UCASE(SUBSTR(`posts`.`title`,1,1)) as `char`');
$filters=[];
View::share('title', 'Блог');
View::share('subtitle', 'Здесь вы найдете что-нибудь интересное');
if($path){
$uri = Str::afterLast($path,'/');
$cat=Categories::where('uri','=',$uri)->with(['ancestors','descendants','ancestors.ancestors'])->firstOrFail();
abort_if($cat->getPath()!=$path,404);
$cat->buildBreadcrumbs();
$cat_ids = clone $cat->descendants;
$cat_ids = $cat_ids->push($cat)->pluck('id')->toArray();
$posts=$posts->whereIn('category_id',$cat_ids);
$letters = $letters->whereIn('category_id',$cat_ids);
$filters['path']=$path;
View::share('title', 'Блог / Категория: '.$cat->name);
View::share('subtitle', $cat->subtitle);
}
//Фильтр по букве названия
if (preg_match('/^[0-9a-zа-я]{1,1}$/iuU', request()->get('char', null))) {
$filters['char'] = Str::upper(request()->get('char', null));
$posts->where(DB::raw('UCASE(SUBSTR(`posts`.`title`,1,1))'),'=',$filters['char']);
$char=Posts::selectRaw('? as `char`',[$filters['char']??'*']);
$letters=$letters->union($char);
}
$page=LengthAwarePaginator::resolveCurrentPage();
$rpp=20;
$cnt= clone $posts;
$cnt=$cnt->count();
$total=ceil($cnt/$rpp);
if( $page>$total){
$page=$total;
}
$offset=( $page-1)*$rpp;
$posts=$posts->with(['user','tags','category','category.ancestors'])->limit($rpp)->offset($offset)->get();
$posts=new LengthAwarePaginator($posts,$cnt,$rpp, $page,['path'=>url()->current()]);
$letters= $letters->distinct()->orderBy('char','asc')->get();
return view('posts.list',compact('posts','letters','filters'));
}
}
T
Н