【发布时间】:2017-12-20 20:26:13
【问题描述】:
我是 laravel 的新手,遇到了一些麻烦。我尝试获取存储在两个不同表中的数据并显示它们:
News.php(模型)
public static function Data($category) {
$perPage = config('var.news.perPage');
if ($category) {
$news = News::orderBy('id', 'desc')->where('category', $category)->SimplePaginate($perPage);
} else {
$news = News::orderBy('id', 'desc')->SimplePaginate($perPage);
}
return $news;
}
这就是我从结构是的 News 表中获取所有数据的方式:
id, title, body, created_at updated_at, created_by, updated_by, category
类别列包含以逗号分隔的值,例如1,2,3,4
现在,我有另一个表 News_Cat,它有 id, name 列。
在另一种方法中,我尝试根据存储在新闻表的类别列中的值来获取过滤器名称
public static function getFilterNames($id) {
$filters = DB::table('News_Cat')
->select('News_Cat.name as name')
->leftJoin('News', DB::raw('CAST(News_Cat.id as nvarchar)'), DB::raw('ANY(SELECT(News.category))'))
->where('News.id', $id)
->get();
return $filters;
}
但是,它完全不起作用。我试图实现的是在 view.blade 中将过滤器名称显示为来自 News_Cat 的指定过滤器的“名称”值
@if($news->count())
@foreach($news as $article)
<a href="{{ route('news.show', $article->id) }}" class="item angled-bg" data-filters="{{ $filters }}">
<div class="row">
所以结果我会得到例如data-filters="news, update, hot, latest"> 改为 data-filters="1,2,3,4">
谢谢
【问题讨论】: