Files
new_ine/app/Helpers/function.php
2021-10-09 13:12:45 +08:00

154 lines
3.4 KiB
PHP

<?php
use App\Models\Advert;
use App\Models\Category;
use App\Models\Article;
use App\Models\Video;
function getOneCategory($categoryId, $return = '')
{
$category = Category::find($categoryId);
if ($category) {
if ($return) {
return $category->{$return};
}
return $category;
}
return new Category;
}
/**
* Notes: 获取文章分类详情
* @Author: 玄尘
* @Date : 2020/9/10 13:21
* @param $categoryId
* @param string $result
* @return \App\Models\Article
*/
function getOneArticleBYCate($categoryId, $result = '')
{
$info = Article::where('status', 1)
->ByCategory($categoryId)
->latest('sort')->latest()->first();
if ($info) {
if ($result) {
return $info->{$result};
}
return $info;
} else {
return '';
}
return new Article;
}
/**
* Notes: 获取分类下的文章
* @Author: 玄尘
* @Date : 2020/9/10 10:05
* @param $categoryId
* @param int $take
* @param string $mark
* @return \App\Models\Article
*/
function getArticlesBYCate($categoryId, $take = 8, $mark = 'one')
{
if ($mark == 'one') {
$articles = Article::where('status', 1)
->ByCategory($categoryId)
->latest('sort')
->latest()
->take($take)
->get();
} else {
$cate = Category::find($categoryId);
$ids = $cate ? $cate->getAllChildrenId() : [];
$articles = Article::where('status', 1)
->ByCategory($ids)
->latest('sort')
->latest()
->take($take)
->get();
}
return $articles;
}
//获取子分类
function getCateChild($categoryId)
{
return Category::where('status', 1)
->where('parent_id', $categoryId)
->orderBy('order', 'asc')
->get();
}
//获取顶级分类
function getTopCate($categoryId)
{
$parent = Category::find($categoryId);
while ($parent->parent_id != 0) {
$parent = $parent->parent;
}
return $parent;
}
/**
* Notes: 根据分类获取一张图片
* @Author: 玄尘
* @Date : 2021/10/8 13:53
* @param $categoryId
* @param string $result
* @return string
*/
function getOneAdvertByCate($categoryId, $result = '')
{
$info = Advert::where('category_id', $categoryId)
->latest('sort')
->latest()
->first();
if ($info) {
if ($result) {
return $info->{$result};
}
return $info;
} else {
return '';
}
}
function getVideoByCate($categoryId, $result = '')
{
$video = Video::where('category_id', $categoryId)
->latest()
->first();
if ($video) {
if ($result) {
return $video->{$result};
}
return $video;
} else {
return '';
}
}
function getVideosByCate($categoryId, $take = '8')
{
return Video::where('category_id', $categoryId)
->latest()
->take($take)
->get();
}