2016-12-25 51 views
4

我有一个网站,我发布食谱。他们每个人都有一个类别,我想显示2-3个职位与该职位相同的类别。 我如何建立一个查询来显示它?我有一个Post Model和Category模型,它们之间有一个belongsToMany关系,并且它填充了一个数据透视表,我将这些类别绑定到了一个帖子。根据laravel中的相同类别创建“相关”部分

这是我的BController中的函数,这是将数据传递给用户可以访问和查看的视图的函数。

public function slug(Request $request, $slug) 

    { 
     if (Auth::check()) { 
      $fav = DB::table('post_user')->whereUserId(Auth::id())->pluck('post_id')->all(); 
     } 
     /*get search query from slug page*/ 
     $query=$request->get('q'); 
     /*display the results of the search*/ 
     if ($query) { 
      $posts = $query ? Post::search($query) 
      ->orderBy('id','desc') 
      ->paginate(7) : Post::all(); 
      return view('home', compact('posts','fav')); 
     } 
     /*the $url veriable is for share buttons*/ 
      else { 
      $url = $request->url(); 
      $post = Post::where('slug', '=', $slug)->first(); 
      return view('b.s', compact('post','url')); 

     } 
    } 

这是我的Post模型:

public function categories(){ 
    return $this->belongsToMany('App\Category'); 
} 

这是分类模式:

public function posts(){ 
    return $this->belongsToMany('App\Post'); 
} 

枢轴表像这样:

  $table->increments('id'); 
      $table->integer('post_id')->unsigned(); 

      $table->foreign('post_id')->references('id') 
      ->on('posts')->onDelete('cascade'); 

      $table->integer('category_id')->unsigned()->nullable(); 
      $table->foreign('category_id')->references('id') 
      ->on('categories')->onDelete('cascade'); 

回答

4

您可以使用whereHas对相关表作为添加约束:

// get the post usnig slug 
$post = Post::where('slug', '=', $slug)->first(); 

// get the related categories id of the $post 
$related_category_ids = $post->categories()->pluck('categories.id'); 

// get the related post of the categories $related_category_ids 
$related_posts = Post::whereHas('categories', function ($q) use($related_category_ids) { 
     $q->whereIn('category_id', $related_category_ids) 
    }) 
    ->where('id', '<>', $post->id) 
    ->take(3) 
    ->get(); 

更新

$related_posts传递给您的视图并将其用作:

@foreach ($related_posts as $related_post) 
    <li>{{ related_post->title }}</li> 
@endforeach 
+0

你的代码让我这个:'#{#247▼ #items:array:1 [▼ 0 =>“12” ] }'我怎样才能访问这个实际的职位?我很抱歉在这里做菜,但我是。在几天前开始这对夫妇,仍然在学习课程。谢谢。 – GabMic

+0

你在哪一行得到这个? –

+0

我得到它时,我做'dd($ related_category_ids);' – GabMic

1

可能的解决方案是包含一段代码以获取所需的类别。

如果我正确理解你的模型,你可以有几个类别。所以,我们需要把你的帖子的所有类别,并保持唯一的ID;我们必须排除当前对象:)

$post = Post::where('slug', '=', $slug)->first(); 

// get Category IDs. There are others ways to do it. 
$categoriesId = []; 
foreach($post->categories as $category) { 
    $categoriesId[] = $cateogyr->id; 
} 

// get similar posts 
$others = Post:: 
    whereIn('categories', $categoriesId) 
    ->where('id', '!=', $post->id) 
    ->take(3) 
    ->get(); 

在你的情况的帖子ID,你有一个数据透视表:

$others = Post:: 
    with(array('YourPivot' => function($query) use($categoriesId) 
    { 
     whereIn('categories', $categoriesId) 
    }) 
    ->where('id', '!=', $post->id) 
    ->take(3) 
    ->get(); 
+0

您能解释法语吗? – GabMic

+0

对不起。我不知道我为什么用法语写作。我将翻译^^ –

+0

谢谢,但我得到这个错误:'调用未定义的方法照亮\数据库\查询\生成器:: andWhere()' – GabMic