Php 如何显示与表单输入匹配的特定类别的文章-Laravel
我无法根据搜索结果的类别限制搜索/显示结果Php 如何显示与表单输入匹配的特定类别的文章-Laravel,php,mysql,laravel-5.1,Php,Mysql,Laravel 5.1,我无法根据搜索结果的类别限制搜索/显示结果 foreach ($articles as $article){ //get the article's categories ids $article->categories->lists('id'); } 我有一种多对多的关系。一篇文章有很多类别,一个类别有很多文章。用户在表单中输入地址并选择1-3个类别。然后,我希望将这些用户指定的类别与数据库中具有这些类别且仅显示这些特定文章的文章进行匹配 我一直在考虑很多问题
foreach ($articles as $article){
//get the article's categories ids
$article->categories->lists('id');
}
我有一种多对多的关系。一篇文章有很多类别,一个类别有很多文章。用户在表单中输入地址并选择1-3个类别。然后,我希望将这些用户指定的类别与数据库中具有这些类别且仅显示这些特定文章的文章进行匹配
我一直在考虑很多问题,比如,似乎无法让事情顺利进行。我的主要错误是我得到了$categories的未定义属性
foreach ($articles as $article){
//get the article's categories ids
$article->categories->lists('id');
}
在我添加分类内容之前,它是有效的,我可以显示在一定距离内的文章列表。我使用的是Laravel5.1
数据库表:
“文章”
(身份证、头衔等)
“类别”
(身份证、姓名)
“物品类别”
(物品编号、类别编号)
这是我的密码:
文章范本:
public function categories()
{
return $this->belongsToMany('App\Category')->withTimestamps();
}
public function getCategoryListAttribute()
{
return $this->categories->lists('id')->all();
}
类别模型:
public function articles()
{
return $this->belongsToMany('App\Article')->withTimestamps();
}
物品管理员
public function index(Request $request)
{
$lat = $request->get('lat');
$lng = $request->get('lng');
<--! THIS GETS THE USER DEFINED CATEGORY CHOICES -->
$categoryChoice = $request->get('categoryList');
$distance = 1;
$query = Article::getByDistance($lat, $lng, $distance);
if(empty($query)) {
return redirect()->action('HomeController@index');
}
$ids = [];
//Extracts the alticle/store id's
foreach($query as $q)
{
array_push($ids, $q->id);
}
<--! Get the listings that match the returned ids -->
$results = DB::table('articles')
->whereIn( 'id', $ids)
->orderBy('title', 'DESC')
->paginate(6);
$articles = $results;
<--! THIS IS TO GET CATEGORY IDS OF NEARBY STORES - NOT WORKING ->
$categoryMatches = [];
foreach ($articles->categories as $category){
array_push($categoryMatches, $category->pivot->category_id);
}
<--! THIS WILL IDENTIFY WHETHER THE CATEGORIES OF NEARBY STORES MATCH THE CATEGORIES CHOSEN IN THE FORM - NOT WORKING -->
$articles = $articles->whereIn($categoryMatches, $categoryChoice);
return view('articles.index', compact('categories', 'days'))->withArticles($articles);
}
公共功能索引(请求$Request)
{
$lat=$request->get('lat');
$lng=$request->get('lng');
$categoryChoice=$request->get('categoryList');
$distance=1;
$query=Article::getByDistance($lat、$lng、$distance);
if(空($query)){
return redirect()->action('HomeController@index');
}
$ids=[];
//提取高度层/存储id
foreach($q查询)
{
数组推送($ids,$q->id);
}
$results=DB::表('articles')
->其中('id',$id)
->orderBy('title','DESC')
->分页(6);
$articles=$results;
$categoryMatches=[];
foreach($articles->categories as$category){
数组推送($categoryMatches,$category->pivot->category\u id);
}
$articles=$articles->where($categoryMatches,$categoryChoice);
返回视图('articles.index',compact('categories','days')->withArticles($articles);
}
您的查询返回一组文章。您需要首先迭代文章,因为每篇文章都有许多类别
foreach ($articles as $article){
//get the article's categories ids
$article->categories->lists('id');
}
谢谢这会在哪一点发生?当我使用它时,仍然会得到“未定义属性:stdClass::$categories”。