Make the "Related" section based on the same category in laravel

I have a website where I post recipes. each of them has a category, and I want to display 2-3 more messages with the same category of this message. How can I create a query to display it? I have a Post Model and Category model, which has toMany relationship between them and populates a pivot table that I bind these categories to the message.

This is a function in my BController, it is a function that passes data to views in which users can access and view.

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'));

        }
    }

this is my publication model:

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

This is in Category Category:

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

The summary table looks like this:

            $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
2

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
+4

.

, . , ; :)

$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();

:

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

Source: https://habr.com/ru/post/1664839/


All Articles